【问题标题】:Advanced toggle function to show some items高级切换功能以显示某些项目
【发布时间】:2014-10-08 23:01:55
【问题描述】:

需要一个高级的 javascript 切换功能。 (我没有编写自己的js的经验,希望有人可以帮助我)。

点击 img 时,将另一个 div 从 height=0 变为 height=auto(动画)。 我已经复制了代码,因此需要显示切换 - 仅隐藏子/父 div,而不是使用“i-more”类来切换所有 div。

我做了一个小提琴来展示我想做什么:

EXAMPLE IN FIDDLE HERE >

HTML:

<div class="i-holder">
    <div class="i-left">
        <img class="i-one" src="http://placehold.it/200x120.jpg" />
        <div class="i-more">
            <img src="http://placehold.it/200x120.jpg" />
            <img src="http://placehold.it/200x120.jpg" />
            <img src="http://placehold.it/200x120.jpg" />
        </div>
    </div>
    <div class="i-right">
        <h2>Header here</h2>
        <p>Text here text here text here text here text here text here text  here...</p>
        <img class="i-btn-more" src="http://placehold.it/40x30.jpg" alt="read more" />
        <div class="i-more">
            <p>More text here more text more text more text more text more text more text more text more text more text here text here text here text here text here text here text here text here text here text here text here.</p>
            <p>More text here more text more text more text more text more text more text more text more text more text here text here text here text here text here text here text here text here text here text here text here.</p>
            <a href="javascript();" class="less">Show less</a>
        </div>
    </div>
    <div class="clear"></div>

    <div>
        <h2>More stuff like above here...</h2>
        <p>A duplicate from above.</p>
        <p>when click on the i-btn-more only that text and thumb will be visible and not all divs with that same class.</p>
    </div>

</div>

CSS:

.i-more{height:0; overflow:hidden;}

.i-holder{width:600px; margin:20px; position:relative;}
.i-left img{display:block; margin-bottom:10px;}

.i-left{float:left; width:200px; background:#fc1; min-height:130px; }
.i-right{float:right; width:320px; padding:0 50px 10px 10px; background:#6d1; min-height:130px; }

.i-btn-more{position:absolute; cursor:pointer; margin:-40px 0 0 330px;}
.i-more{clear:both;}

.clear{clear:both;}

【问题讨论】:

    标签: javascript show-hide css-animations


    【解决方案1】:

    将您的样式放在一个类中,例如切换显示:

    .shown{
        display:block;
    }
    .hidden{
        display:none;
    }
    

    然后你只需要使用 jQuery 选择器找到你想要的元素。

    所以:

    $('.i-btn-more').click(function(){
        $('.classToHide').toggleClass('shown').toggleClass('hidden');
    });
    

    确保它以.show 类(如果它正在显示)或.hidden 类(如果它默认隐藏)开头。然后toggleClass函数将打开和关闭类;因此,样式。

    要选择某些东西而不是某些东西,请使用 jQuery .not() 函数来过滤选择器。例如,$('.more').not('p').toggleClass('hidden'); 将切换为隐藏所有具有类 .more 的类,除了 &lt;p&gt; 段落。

    【讨论】:

    • 谢谢!,它现在部分工作:jsfiddle.net/fourroses666/vfyr1z0v/6 你知道如何让它在具有相同类的多个项目上工作吗?详情见小提琴
    【解决方案2】:
    $('.i-btn-more, .i-btn-less').on('click', function (e) {
        e.preventDefault();
        $(this).parents('.i-holder').find('.i-more').toggleClass('shown hidden');
    });
    

    或此代码:

    ;(function() {
    
        'use strict';
    
        $(function() {
            var $containers = $('.i-holder');
    
            $containers.each(function() {
                var $container = $(this),
                    $more = $container.find('.i-more'),
                    $handlers = $container.find('.i-btn-more, .i-btn-less');
    
                $handlers.on('click', function() {
                    $more.toggleClass('shown hidden');
                    e.preventDefault();
                });
            });
        });
    
    })(jQuery);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-27
      • 2018-06-14
      • 2018-09-06
      • 1970-01-01
      • 1970-01-01
      • 2022-05-17
      • 1970-01-01
      • 2010-10-18
      相关资源
      最近更新 更多