我想获得一组淡入淡出使用jQuery。我有一个解决方案,虽然它看起来很笨重。有没有办法简化我的代码?我很新的jQuery,所以任何帮助非常感谢!jquery淡入淡出div
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:
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:
$(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链接是在这里:
http://codepen.io/mikehdesign/pen/eNpxRQ
我还在想着是否使用'href'以代替数据属性比较好。使用'href'需要'event.preventDefault();',但看起来更接近代码的作用。顺便说一句,'$(this).attr('href')'可以被替换为更短的'this.hash',不是吗?还有一件事:''ID现在没用,所以为什么不删除它们? – Regent