[英]Jquery fade divs in and out
I am trying to get a set of divs to fade in and out using jQuery. 我正在尝试使用jQuery使一组div淡入和淡出。 I have a solution, though it seems very clunky.
我有一个解决方案,尽管看起来很笨拙。 Is there a way to streamline my code?
有没有一种方法可以简化我的代码? I am pretty new to jQuery so any help greatly appreciated!
我是jQuery的新手,因此非常感谢您的帮助!
HTML: HTML:
<a class="button" href="javascript:void(0)" id="onelink">Show Div One</a>
<a class="button" href="javascript:void(0)" id="twolink">Show Div Two</a>
<a class="button" href="javascript:void(0)" id="threelink">Show Div Three</a>
<a class="button" href="javascript:void(0)" id="fourlink">Show Div Four</a>
<div class="content">
<div id="one" class="thing">One</div>
<div id="two" class="thing">Two</div>
<div id="three" class="thing">Three</div>
<div id="four" class="thing">Four</div>
</div>
SCSS: SCSS:
a.button {
text-decoration: none;
color: #000;
display: inline-block;
padding: 10px;
margin: 20px;
border: 1px solid #000;
@include transition (all 0.2s);
&:hover {
border: 1px solid #dadada;
background-color: #f5f5f5;
}
}
div.thing {
border: 1px solid #BADA55;
background-color: #dadada;
padding: 30px;
text-align: center;
width: 200px;
margin: 20px;
position: absolute;
display: none;
&#one {
display: block;
}
}
JQUERY: JQUERY:
$(document).ready(function() {
$("#onelink").click(function() {
$("div#one").fadeIn();
$("div#two, div#three, div#four").fadeOut();
});
$("#twolink").click(function() {
$("div#two").fadeIn();
$("div#one, div#three, div#four").fadeOut();
});
$("#threelink").click(function() {
$("div#three").fadeIn();
$("div#one, div#two, div#four").fadeOut();
});
$("#fourlink").click(function() {
$("div#four").fadeIn();
$("div#one, div#two, div#three").fadeOut();
});
});
Codepen link is here: Codepen链接在这里:
http://codepen.io/mikehdesign/pen/eNpxRQ http://codepen.io/mikehdesign/pen/eNpxRQ
Use data-attributes
, so suppose #onelink
opens div#one
- add data-id = "one"
to it ans so on., 使用
data-attributes
,因此假设#onelink
打开div#one
#onelink
添加data-id = "one"
,依此类推。
<a class="button" href="" data-id="one">Show Div One</a>
<a class="button" href="" data-id="two">Show Div Two</a>
<a class="button" href="" data-id="three">Show Div Three</a>
<a class="button" href="" data-id="four">Show Div Four</a>
EDIT: 编辑:
The javascript:void(..)
can be removed and the default event of <a>
can be handled by using event.preventDefault()
. 可以删除
javascript:void(..)
并使用event.preventDefault()
处理默认事件<a>
。
Or maybe just remove the href
and handle the cursor
in CSS. 或者也许只是删除
href
并在CSS中处理cursor
。
Also, as @Regent suggests, the IDs id="onelink"
.. are no more required as the solution works on classes. 而且,正如@Regent所建议的那样,由于该解决方案适用于类,因此不再需要ID
id="onelink"
..。
$(document).ready(function() {
$(".button").click(function(event) {
event.preventDefault();
var $divtoRender = $("#"+$(this).data('id'));
$divtoRender.fadeIn();
$("div.thing:visible").not($divtoRender).fadeOut();
});
});
Instead of adding separate handlers for each button, you can use a common handler and use the href
to specify which thing
to be displayed. 而不是添加单独的处理程序为每个按钮,你可以使用通用的处理程序,并使用
href
来指定哪些thing
来显示。
<a class="button" href="#one">Show Div One</a>
<a class="button" href="#two">Show Div Two</a>
<a class="button" href="#three">Show Div Three</a>
<a class="button" href="#four">Show Div Four</a>
<div class="content">
<div id="one" class="thing">One</div>
<div id="two" class="thing">Two</div>
<div id="three" class="thing">Three</div>
<div id="four" class="thing">Four</div>
</div>
then 然后
$(document).ready(function () {
var $things = $('.thing');
$(".button").click(function (e) {
e.preventDefault();
var $div = $($(this).attr('href'));
$div.stop().fadeIn();
$things.not($div).stop().fadeOut();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.