简体   繁体   English

如何提高效率? -多个jQuery切换

[英]How can I make this more efficient? - Multiple jQuery Toggles

I have a handful of images working as jQuery toggle switches - Showing or hiding an element on a click event. 我有一些图像可以用作jQuery切换开关-在click事件上显示或隐藏元素。

The code below mostly works as intended, but I'm fairly certain I'm using a lot of unnecessary stuff to make it happen. 下面的代码通常可以按预期工作,但是我可以肯定地说我正在使用很多不必要的东西来实现它。 Any suggestions on streamlining? 有任何简化建议吗?

Also, any suggestions on closing (toggle to hide) any visible elements that had already been toggled to visible? 另外,关于关闭(切换为隐藏)已经切换为可见的任何可见元素的任何建议? As it works now, if the user clicks on the first image and then the second image, it stacks the newly visible elements on top of each other. 到现在为止,如果用户单击第一幅图像,然后单击第二幅图像,它将新可见的元素彼此堆叠。 Ideally, I'd like to close (hide) the element from the first toggle when the second toggle is clicked and/or close any open elements on a scroll event. 理想情况下,我想在单击第二个切换开关时关闭(隐藏)第一个切换开关的元素和/或关闭滚动事件上的所有打开的元素。

The code below is abridged. 下面的代码是节略的。 There's actually 10+ toggles which can get a little unwieldy if the user has opened a bunch of them. 实际上有10个以上的切换开关,如果用户打开了很多开关,它们可能会变得有些笨拙。

EDIT - Based on the answers below, I used common classes to tighten up the jQuery. 编辑-根据以下答案,我使用了常见的类来加强jQuery。 The solution is also closing(hiding) any other open(visible) elements when the next toggle is clicked, but only within that toggle's parent. 当单击下一个切换时,该解决方案还关闭(隐藏)任何其他打开(可见)的元素,但仅在该切换的父对象中。 Since there are multiple UL's broken up between multiple parent div's, clicking to close wihtin "column2" will close any visible elements within "column2", but not within another column (ie: an open/visible element from column1) 由于多个父div之间存在多个UL分解,因此单击以关闭wihtin“ column2”将关闭“ column2”中的所有可见元素,但不会关闭另一列中的可见元素(即:column1中的open / visible元素)

UPDATED CODE: 更新的代码:

  $(".show-wine").click(function(){ // Hide all wines except for the one that was clicked $(this).parent().siblings().find(".wine-text").hide(); // Show or hide the one that was clicked $(".wine-text", $(this).parent()).toggle(); }); $(".hide-wine").click(function(){ $('.wine-text').hide(); // Hide all wines }); 
 .wine-text { display:none; position:fixed; top:3em; left:1.5em; width:35em; padding:1em; height:auto; min-height:30em; background: rgba(0,0,0,0.95); color:#FFF; border-radius:4px; line-height:1.25em; text-align:left; } .wine-tiles li img:hover { cursor:pointer; opacity:.7; } .wine-tiles li img .unhide:hover .wine-text { display:block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="column1"> <ul class="wine-tiles"> <li> <img src="images/labels/image.jpg" class="show-wine unhide bounce" alt="Img 3"/> <span class="wine-text"> <span class="wine-text-title centered">Wine#1</span><br> <span class="wine-text-winery centered">Big White House</span><br><br> <span class="wine-text-copy">Lorem Ipsum</span> <br/><br/> <button class="button hide-wine">Close</button> </span> <h2>Title1</h2> </li> <li> <img src="images/labels/image.jpg" class="show-wine unhide bounce" alt="Img 3"/> <span class="wine-text"> <span class="wine-text-title centered">Wine#2</span><br> <span class="wine-text-winery centered">Big White House</span><br><br> <span class="wine-text-copy">Lorem Ipsum</span> <br/><br/> <button class="button hide-wine">Close</button> </span> <h2>Title2</h2> </li> <li> <img src="images/labels/image.jpg" class="show-wine unhide bounce" alt="Img 3"/> <span class="wine-text"> <span class="wine-text-title centered">Wine#3</span><br> <span class="wine-text-winery centered">Big White House</span><br><br> <span class="wine-text-copy">Lorem Ipsum</span> <br/><br/> <button class="button hide-wine">Close</button> </span> <h2>Title3</h2> </li> </ul> </div> <div id="column2"> <ul class="wine-tiles"> <li> <img src="images/labels/image.jpg" class="show-wine unhide bounce" alt="Img 3"/> <span class="wine-text"> <span class="wine-text-title centered">Wine#4</span><br> <span class="wine-text-winery centered">Big White House</span><br><br> <span class="wine-text-copy">Lorem Ipsum</span> <br/><br/> <button class="button hide-wine">Close</button> </span> <h2>Title4</h2> </li> <li> <img src="images/labels/image.jpg" class="show-wine unhide bounce" alt="Img 3"/> <span class="wine-text"> <span class="wine-text-title centered">Wine#5</span><br> <span class="wine-text-winery centered">Big White House</span><br><br> <span class="wine-text-copy">Lorem Ipsum</span> <br/><br/> <button class="button hide-wine">Close</button> </span> <h2>Title5</h2> </li> <li> <img src="images/labels/image.jpg" class="show-wine unhide bounce" alt="Img 3"/> <span class="wine-text"> <span class="wine-text-title centered">Wine#6</span><br> <span class="wine-text-winery centered">Big White House</span><br><br> <span class="wine-text-copy">Lorem Ipsum</span> <br/><br/> <button class="button hide-wine">Close</button> </span> <h2>Title6</h2> </li> </ul> </div> 

ORIGINAL CODE: 原始代码:

 $("#show-wine").click(function(){ $(".wine-text").toggle(); }); $("#show-wine2").click(function(){ $(".wine-text2").toggle(); }); $("#show-wine3").click(function(){ $(".wine-text3").toggle(); }); 
 .wine-text, .wine-text2, .wine-text3, .wine-text4, .wine-text5, .wine-text6, .wine-text7, .wine-text8, .wine-text9 { display:none; position:fixed; top:3em; left:1.5em; width:35em; padding:1em; height:auto; min-height:30em; background: rgba(0,0,0,0.95); color:#FFF; border-radius:4px; line-height:1.25em; text-align:left; } .wine-tiles li img:hover { cursor:pointer; opacity:.7; } .wine-tiles li img .unhide:hover .wine-text { display:block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="column1"> <ul class="wine-tiles"> <li> <img src="images/labels/image.jpg" id="show-wine" class="unhide bounce" alt="Img 1"/> <span class="wine-text"> <span class="wine-text-title centered">Wine#1</span><br> <span class="wine-text-winery centered">Big White House</span><br><br> <span class="wine-text-copy">Lorem Ipsum</span> <br/><br/> <button id="hide-wine" class="button">Close</button> </span> <h2>Title</h2> </li> <li> <img src="images/labels/image.jpg" id="show-wine2" class="unhide bounce" alt="Img 2"/> <span class="wine-text2"> <span class="wine-text-title centered">Wine#2</span><br> <span class="wine-text-winery centered">Big White House</span><br><br> <span class="wine-text-copy">Lorem Ipsum</span> <br/><br/> <button id="hide-wine" class="button">Close</button> </span> <h2>Title</h2> </li> <li> <img src="images/labels/image.jpg" id="show-wine3" class="unhide bounce" alt="Img 3"/> <span class="wine-text3"> <span class="wine-text-title centered">Wine#3</span><br> <span class="wine-text-winery centered">Big White House</span><br><br> <span class="wine-text-copy">Lorem Ipsum</span> <br/><br/> <button id="hide-wine" class="button">Close</button> </span> <h2>Title</h2> </li> </ul> </div> <div id="column2"> <ul class="wine-tiles"> <li> <img src="images/labels/image.jpg" id="show-wine" class="unhide bounce" alt="Img 1"/> <span class="wine-text"> <span class="wine-text-title centered">Wine#4</span><br> <span class="wine-text-winery centered">Big White House</span><br><br> <span class="wine-text-copy">Lorem Ipsum</span> <br/><br/> <button id="hide-wine" class="button">Close</button> </span> <h2>Title</h2> </li> <li> <img src="images/labels/image.jpg" id="show-wine2" class="unhide bounce" alt="Img 2"/> <span class="wine-text2"> <span class="wine-text-title centered">Wine#5</span><br> <span class="wine-text-winery centered">Big White House</span><br><br> <span class="wine-text-copy">Lorem Ipsum</span> <br/><br/> <button id="hide-wine" class="button">Close</button> </span> <h2>Title</h2> </li> <li> <img src="images/labels/image.jpg" id="show-wine3" class="unhide bounce" alt="Img 3"/> <span class="wine-text3"> <span class="wine-text-title centered">Wine#6</span><br> <span class="wine-text-winery centered">Big White House</span><br><br> <span class="wine-text-copy">Lorem Ipsum</span> <br/><br/> <button id="hide-wine" class="button">Close</button> </span> <h2>Title</h2> </li> </ul> </div> 

Simply give a common class to the element that trigger the toggle and a common class on the toggled element. 只需为触发切换的元素提供一个通用类,并为切换元素提供一个通用类。 Then, for your specific case, you could use that : 然后,对于您的特定情况,可以使用:

$(".show-wine").click(function(){  
    var $target = $(this).next('.wine-text').toggle();
    $('.wine-text').not($target).hide()
});

That will not work if you slightly change you DOM. 如果您稍微更改DOM,那将不起作用。 For a more general case, you'd need to target the common parent first : 对于更一般的情况,您需要首先定位公共父对象:

$(".show-wine").click(function(){  
    var $target = $(this).closest('li').find('.wine-text').toggle();
    $('.wine-text').not($target).hide()
});

 $(".show-wine").click(function(){ var $target = $(this).closest('li').find('.wine-text').toggle(); $('.wine-text').not($target).hide() }); 
 .wine-text { display:none; position:fixed; top:3em; left:1.5em; width:35em; padding:1em; height:auto; min-height:30em; background: rgba(0,0,0,0.95); color:#FFF; border-radius:4px; line-height:1.25em; text-align:left; } .wine-tiles li img:hover { cursor:pointer; opacity:.7; } .wine-tiles li img .unhide:hover .wine-text { display:block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="column1"> <ul class="wine-tiles"> <li> <img src="images/labels/image.jpg" class="show-wine unhide bounce" alt="Img 3"/> <span class="wine-text"> <span class="wine-text-title centered">Wine#1</span><br> <span class="wine-text-winery centered">Big White House</span><br><br> <span class="wine-text-copy">Lorem Ipsum</span> <br/><br/> <button class="button hide-wine">Close</button> </span> <h2>Title1</h2> </li> <li> <img src="images/labels/image.jpg" class="show-wine unhide bounce" alt="Img 3"/> <span class="wine-text"> <span class="wine-text-title centered">Wine#2</span><br> <span class="wine-text-winery centered">Big White House</span><br><br> <span class="wine-text-copy">Lorem Ipsum</span> <br/><br/> <button class="button hide-wine">Close</button> </span> <h2>Title2</h2> </li> <li> <img src="images/labels/image.jpg" class="show-wine unhide bounce" alt="Img 3"/> <span class="wine-text"> <span class="wine-text-title centered">Wine#3</span><br> <span class="wine-text-winery centered">Big White House</span><br><br> <span class="wine-text-copy">Lorem Ipsum</span> <br/><br/> <button class="button hide-wine">Close</button> </span> <h2>Title3</h2> </li> </ul> </div> <div id="column2"> <ul class="wine-tiles"> <li> <img src="images/labels/image.jpg" class="show-wine unhide bounce" alt="Img 3"/> <span class="wine-text"> <span class="wine-text-title centered">Wine#4</span><br> <span class="wine-text-winery centered">Big White House</span><br><br> <span class="wine-text-copy">Lorem Ipsum</span> <br/><br/> <button class="button hide-wine">Close</button> </span> <h2>Title4</h2> </li> <li> <img src="images/labels/image.jpg" class="show-wine unhide bounce" alt="Img 3"/> <span class="wine-text"> <span class="wine-text-title centered">Wine#5</span><br> <span class="wine-text-winery centered">Big White House</span><br><br> <span class="wine-text-copy">Lorem Ipsum</span> <br/><br/> <button class="button hide-wine">Close</button> </span> <h2>Title5</h2> </li> <li> <img src="images/labels/image.jpg" class="show-wine unhide bounce" alt="Img 3"/> <span class="wine-text"> <span class="wine-text-title centered">Wine#6</span><br> <span class="wine-text-winery centered">Big White House</span><br><br> <span class="wine-text-copy">Lorem Ipsum</span> <br/><br/> <button class="button hide-wine">Close</button> </span> <h2>Title6</h2> </li> </ul> </div> 

I'm not sure if this is the most efficiënt way (there are multiple ways to do it ofcourse), but this would be one of them: 我不确定这是否是最有效的方法(当然有多种方法可以做到),但这将是其中一种:

http://jsfiddle.net/zah9a7xp/2/ http://jsfiddle.net/zah9a7xp/2/

By selecting the images by class and using it's parent you can get javascript code like this: 通过按类别选择图像并使用其父级,您可以获取如下的javascript代码:

$(".show-wine").click(function(){
    var $target = $(".wine-text", $(this).parent());
    // Hide all wines except for the one that was clicked
    $('.wine-text').not($target).hide();
    // Show or hide the one that was clicked
    $target.toggle();
});
$(".hide-wine").click(function(){
    $('.wine-text').hide(); // Hide all wines
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM