1

我编写了一个小脚本,可以通过单击展开和折叠(查看更多/更少)。我的问题是我有“点击我”按钮,当我点击它时,div 是动态创建的。但是当 div 添加到 DOM 时,容器不会自动扩展,因为给定了高度。将高度设置为单击时自动将解决此问题,但还有其他可能的方法来解决此问题。

JSFiddle - http://jsfiddle.net/7ntQR/

代码

(function($) {
    $.fn.truncateText = function() {
        this.each(function() {

            var el = $(this);
            var height = el.height();       //get the current height
            el.data("dht",height);          //use jquery data store to store the original height
            el.css({                        //set the height to some pixel... 
                'height':'40px'
            }); 

            var btnState = 'collapse';

            var ix = $('<div>', {
                'class':'seeMore'
            }).appendTo($(el.parent()));

            var sm = $('<span>').html('more')
            .appendTo(ix)
            .on('click' , function() {
                if(btnState == 'collapse') {                 
                        btnState = 'expand';                            
                        el.animate({
                            'height':'40px'                         
                        },500);     
                }else {                                                     
                        btnState = 'collapse';
                        el.animate({
                            'height':el.data("dht")                         
                        },500);
                }   
            });


            btnState = 'expand';

            function expand(elm) {
                elm.html('SEE LESS');                                           
            }

            function collapse(elm) {
                elm.html('more');
            }




        }); 
    };

})(jQuery);

最初使用 data() 存储 div 的原始高度,然后将 div 的高度更改为 40px。

4

2 回答 2

0

我建议您在生成 div 时获取其大小。例如,获取$('some div id').height();And fix this height 并将其添加到要扩展的原始 div 中。这很容易理解和添加。但我还是建议你去height:auto

于 2012-07-06T03:56:12.830 回答
0

您可以使用纯 css 来完成,并将 html 保留在适当的位置。这里是一个JS fiddle(IE6,7,8上不会显示越来越少,内容会被展开)

http://jsfiddle.net/6sj4e/45/

.text {
    height: 50px;
    overflow: hidden;
    height: 100%\9;
  }

input.less-more,
input.less-more:checked ~ label.more,
input.less-more:not(:checked) ~ label.less {
    display: none;
}

和 html :

<input id="chbox" class="less-more" type="checkbox">
<div class="text">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.</div>
<label for="chbox" class='more'>more</label>
<label for="chbox" class='less'>less</label>
于 2014-04-17T10:26:11.950 回答