“查看更多”功能,较完美的实现

news/2024/7/10 23:45:22 标签: css, js, html, jquery, javascript

“查看更多”功能,较完美的实现

需求:
在这里插入图片描述
文本过多时隐藏文本,用省略号代替,省略号后面有“查看更多”的按钮。
点击“查看更多”后展开所有文本,如下图:
在这里插入图片描述
点击“收起”后恢复原状。
实现

html"><div class="box clearfix">
        <div class="rt more"> 
            <span>...</span>
            <a>查看更多</a>
        </div>
        <div class="text">测试ceshi测试ceshi测试ceshi测试ceshi测试ceshi测试ceshi测试ceshi测试ceshi测试ceshi测试ceshi测试ceshi</div>
        <div class="rt retract">
            <a>收起</a>
        </div>
    </div>
html" title=css>css">.clearfix:after{content:".";display:block;height:0;clear:both;visibility:hidden}
        .box{
            box-sizing: border-box;
            padding-top: 75px;
            width: 100px;
            height: 100px;
            overflow: hidden;
        }
        .text{
            margin-top: -75px;
            font-size: 12px;
            line-height: 25px;
            word-wrap: break-word;
            white-space: normal;
            word-break: break-all;
        }
        .rt{
            float: right;
            font-size: 12px;
            line-height: 25px;
        }
        .rt a{
            color:red;
            cursor: pointer;
        }
        .ha{
            height: auto;
        }
html" title=js>js">$(".rt a").click(function(){
    if($('.more').html" title=css>css("display")!=='none'){
        $(".box").addClass('ha')
        $('.more').hide() 
    }else{
        $(".box").removeClass('ha')
        $('.more').show() 
    }
});

分析
1.“查看更多”按钮实际上是依靠浮动,实现了一个文字环绕的效果。(在文字前面设置浮动,则可以达到文字环绕的效果。当我们给“查看更多”按钮设置右浮动时,效果如下:)
在这里插入图片描述
而省略号代替省略文件的功能,实际上也是依靠文字环绕的效果模拟的。
2.“查看更多”按钮依靠浮动定位,只能定位到在文字的第一行,这时如果给“查看更多”按钮设置margin或者padding,会挤开文字。于是给.box 设置 padding-top:
在这里插入图片描述
再给.text设置margin-top:
在这里插入图片描述
即可达到效果。
/****************************************************************************************************
*************************************2020/09/29更新
****************************************************************************************/
更改:
新增了判断字数是否超出,以及是否显示查看更多的更能。

html" title=css>css">.clearfix:after{content:".";display:block;height:0;clear:both;visibility:hidden}
        .box{
            box-sizing: border-box;
            padding-top: 75px;
            width: 100px;
            height: 100px;
            overflow: hidden;
        }
        .text{
            margin-top: -75px;
            font-size: 12px;
            line-height: 25px;
            word-wrap: break-word;
            white-space: normal;
            word-break: break-all;
        }
        .rt{
            float: right;
            font-size: 12px;
            line-height: 25px;
        }
        .rt a{
            color:red;
            cursor: pointer;
        }
        .ha{
            height: auto;
        }
        .retract{
            display: none;
        }
        .my_more{
            display: none;
        }
html"><div class="box clearfix">
        <div class="rt more my_more"> 
            <span>...</span>
            <a>查看更多</a>
        </div>
        <div class="text">测试ceshi测试ceshi测试ceshi测试ceshi测试ceshi测试ceshi测试ceshi测试ceshi</div>
        <div class="rt retract my_more">
            <a>收起</a>
        </div>
    </div>
javascript">$(function(){ 
    $(".rt a").click(function(){
        if($('.more').html" title=css>css("display")!=='none'){
            $(".box").addClass('ha')
            $('.more').hide() 
            $('.retract').show()
        }else{
            $(".box").removeClass('ha')
            $('.more').show() 
            $('.retract').hide()
        }
    });
    if($('.text').height()>$('.box').innerHeight()){//判断是否需要显示查看更多
        $('.my_more').show()
    }
}); 

效果:
在这里插入图片描述
刚好装满父容器时,不会显示查看更多。


http://www.niftyadmin.cn/n/1355289.html

相关文章

java rtf转doc_PDF转一切,永久免费

PDF格式是我们日常经常会用到的格式&#xff0c;它可以将文字、字型、格式、颜色及独立于设备和分辨率的图形图像等封装在一个文件中。文件占用小&#xff0c;转存方便&#xff0c;跨平台查看且不改变排版&#xff0c;支持矢量图形。但缺点也很明显&#xff0c;不能编辑&#x…

pythonppt_python操作ppt下载

这是python操作ppt&#xff0c;包括了python概述&#xff0c;python基础语法&#xff0c;Python数据类型&#xff0c;条件和循环&#xff0c;函数&#xff0c;模块&#xff0c;面向对象编程&#xff0c;文件相关等内容&#xff0c;欢迎点击下载。 python操作ppt是由红软PPT免费…

JS数字失真问题原理分析

问题&#xff1a; js存储数字类型变量时&#xff0c;数字位数过大时会失真。 原理&#xff1a; JS的数字类型&#xff1a;JS 遵循 IEEE 754 规范&#xff0c;采用双精度存储&#xff08;double precision&#xff09;&#xff0c;占用 64 bit&#xff0c;尾数52位。 因此Ja…

margin-top/bottom,padding-top/bottom百分比取值的问题

一、 1.margin-top/bottom&#xff0c;padding-top/bottom设置为百分比时&#xff0c;取值为父元素宽度的百分比。 2.如果想要取父元素高度的百分比&#xff0c;在父元素上添加代码&#xff1a; writing-mode: vertical-lr;二、 关于为什么为W3C规定&#xff0c;margin和padd…

python解释器的提示符是shell嘛_windows python3.2 shell环境(python叫做解释器)

【进入python的shell 环境&#xff1a;】&#xff08;python里称作命令解释器&#xff0c;windows叫做cmd&#xff0c;unix叫做shell&#xff09; cmd 输入 set path%path%;e:\python2.7 然后输入python 就进入了python的shell界面&#xff0c;类似这样&#xff1a; >>&g…

python爬虫实践报告_Python---python网络爬虫入门实践总结

目录 一、爬虫介绍 爬虫&#xff1a;网络数据采集的程序。 爬虫爬取的数据有什么用&#xff1f; &#xff08;1&#xff09;资料库 &#xff08;2&#xff09;数据分析 &#xff08;3&#xff09;人工智能&#xff1a;人物画像&#xff1b;推荐系统&#xff1a;今日头条、亚马逊…

JS如何准确判断NaN(isNaN函数不可靠问题)

JS如何准确判断NaN&#xff08;isNaN函数不可靠问题&#xff09; 使用isNaN()判断一个变量是否为NaN踩了个坑。 undefined也能够通过isNaN()的判断&#xff0c;难道我还要在判断一下是否为undefined&#xff1f;很明显这样不够优雅 逼格。 那么如何一步到位准确判断一个变量是…

vb.net usb通用协议api_ USB和耳机接口的陷阱?使用之前要注意的兼容性问题

在电脑、手机以及其他种种电子设备上&#xff0c;都会出现各色各样的接口&#xff0c;对应着各自的用途。一般来说&#xff0c;这些接口都能在连接之后提供对应的功能。小雷收到了一个有趣的读者提问&#xff1a;一副3.5mm接口的耳机&#xff0c;可以在苹果手机上正常使用&…