在更多/更少的操作中添加数据

时间:2012-07-06 03:03:46

标签: jquery

我写了一个小脚本,可以通过点击扩展和折叠(查看更多/更少)。我的问题是我有#34;点击我"按钮,当我点击它时,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。

2 个答案:

答案 0 :(得分:0)

我建议你在生成div时获取div的大小。例如,获取$('some div id').height();并修复此高度并将其添加到要展开的原始div。这很容易遵循和添加。但我仍然建议你选择height:auto

答案 1 :(得分:0)

你可以使用纯css并保持html到位。这是一个JS小提琴(越来越少的将不会在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>