• taro超过3行隐藏显示展开功能


    结构:

    1. <View id='desName' className={`${styles.p} ${!isCut ? 'text-cut-4' : ''}`}>
    2. {info?.briefIntroduction}
    3. </View>
    4. <View id='name' className={`${styles.p} ${styles.briefIntroductionNoShow}`}>
    5. {info?.briefIntroduction}
    6. </View>
    7. {isShowMore && (
    8. <View className={styles.open} onClick={handleOpenIsCut}>
    9. {isCut ? '收起详情' : '展开详情'}{' '}
    10. <Image src={icon_push} className={`${styles.icon_push} ${isCut ? styles.rotate : ''}`}></Image>
    11. </View>
    12. )}

    样式:

    1. .p {
    2. font-size: 14px;
    3. color: #333333;
    4. line-height: 24px;
    5. margin-top: 12px;
    6. }
    7. .briefIntroductionNoShow{
    8. position: absolute;
    9. left: 999px;
    10. width: 100%;
    11. }
    12. .text-cut-2,.text-cut-4 {
    13. display: -webkit-box;
    14. -webkit-box-orient: vertical;
    15. overflow: hidden;
    16. word-break: break-all;
    17. }
    18. .text-cut-2 {
    19. -webkit-line-clamp: 2;
    20. }
    21. .text-cut-4 {
    22. -webkit-line-clamp: 4;
    23. }

    逻辑:

    1. const [isShowMore, setIsShowMore] = useState<boolean>(false);
    2. const [info, setInfo] = useState<Partial<infoProp>>({});
    3. const [isCut, setIsCut] = useState<boolean>(false);
    4. const handleOpenIsCut = () => {
    5. setIsCut(!isCut);
    6. };
    7. useEffect(() => {
    8. if (info?.briefIntroduction) {
    9. Taro.nextTick(async () => {
    10. const query = await selectorQueryClientRect('#name');
    11. const queryOld = await selectorQueryClientRect('#desName');
    12. // 4行行高96
    13. if (query.height > queryOld) {
    14. setIsShowMore(true);
    15. }
    16. });
    17. }
    18. }, [info]);
    19. const selectorQueryClientRect = (selector: string): Promise<NodesRef.BoundingClientRectCallbackResult> =>
    20. new Promise(resolve => {
    21. const query = Taro.createSelectorQuery();
    22. query
    23. .select(selector)
    24. .boundingClientRect((res: NodesRef.BoundingClientRectCallbackResult) => {
    25. resolve(res);
    26. })
    27. .exec();
    28. });

  • 相关阅读:
    SQL优化
    计算机毕业设计node.js+Vue+Element电商后台管理系统
    IntelliJ IDEA的快速配置详细使用
    Flowable工作流中会签节点处理回退并清除审批意见
    ⽬标⾏动及稠密环境未知情况下,⽆⼈机跟踪的系统解决⽅案
    MySQL-锁分类-1
    POJ 2991 Crane 线段树
    Mysql中EXPLAIN解读
    一次线上故障:数据库连接池泄露后的思考
    “一篇就够“系列:RxJava 核心解密
  • 原文地址:https://blog.csdn.net/namechenfl/article/details/125537864