jQuery - 帮助组合多个 mouseenter/mouseleave 事件
抱歉,如果这是一个菜鸟问题,但是有没有办法结合这些 mouseenter/mouseleave 事件?
$('.home').mouseenter(function(){
$('#display_home:hidden').fadeIn(400);
}).mouseleave(function(){
$('#display_home').hide()
});
$('.about').mouseenter(function(){
$('#display_about:hidden').fadeIn(400);
}).mouseleave(function(){
$('#display_about').hide();
});
$('.contact').mouseenter(function(){
$('#display_contact:hidden').fadeIn(400);
}).mouseleave(function(){
$('#display_contact').hide();
});
$('.services').mouseenter(function(){
$('#display_services:hidden').fadeIn(400);
}).mouseleave(function(){
$('#display_services').hide();
});
我尝试过不同程序员的不同方法。我试图通过将鼠标悬停在由 mouseenter
函数链接到每个 div 的 li 类上来独立隐藏/显示多个 div,但我是 jQuery 新手,似乎找不到解决方案。我知道必须有一种更干净的方法来做到这一点,只是我还没有找到。任何帮助将不胜感激!
谢谢
html:
<div id="right_nav">
<div id='display_home'><img src="images/gallery/home.png" width="605" height="300" /></div>
<div id='display_about'><img src="images/gallery/about us.png" width="605" height="300" /></div>
<div id='display_contact'><img src="images/gallery/Contact Us.png" width="605" height="300" /></div>
<div id='display_services'><img src="images/gallery/Services.png" width="605" height="300" /></div>
</div>
<div id="thumb">
<ul>
<li class="home"><img src="images/gallery/thumb/home.png" width="82" height="23" /></li>
<li class="about"><img src="images/gallery/thumb/about us.png" width="130" height="24" /></li>
<li class="contact"><img src="images/gallery/thumb/Contact Us.png" width="150" height="23" /></li>
<li class="services"><img src="images/gallery/thumb/Services.png" width="113" height="24" /></li>
</ul>
</div>
Sorry if this is a noob question but is there a way to combine these mouseenter/mouseleave events?
$('.home').mouseenter(function(){
$('#display_home:hidden').fadeIn(400);
}).mouseleave(function(){
$('#display_home').hide()
});
$('.about').mouseenter(function(){
$('#display_about:hidden').fadeIn(400);
}).mouseleave(function(){
$('#display_about').hide();
});
$('.contact').mouseenter(function(){
$('#display_contact:hidden').fadeIn(400);
}).mouseleave(function(){
$('#display_contact').hide();
});
$('.services').mouseenter(function(){
$('#display_services:hidden').fadeIn(400);
}).mouseleave(function(){
$('#display_services').hide();
});
I've tried various methods by various programmers. I'm trying to hide/show multiple divs independently by mousing over the li class linked to each div by the mouseenter
function but I'm new to jQuery and can't seem to find a solution. I know there has to be a cleaner way of doing this, I just haven't found one yet. Any help would be greatly appreciated!
Thanks
html:
<div id="right_nav">
<div id='display_home'><img src="images/gallery/home.png" width="605" height="300" /></div>
<div id='display_about'><img src="images/gallery/about us.png" width="605" height="300" /></div>
<div id='display_contact'><img src="images/gallery/Contact Us.png" width="605" height="300" /></div>
<div id='display_services'><img src="images/gallery/Services.png" width="605" height="300" /></div>
</div>
<div id="thumb">
<ul>
<li class="home"><img src="images/gallery/thumb/home.png" width="82" height="23" /></li>
<li class="about"><img src="images/gallery/thumb/about us.png" width="130" height="24" /></li>
<li class="contact"><img src="images/gallery/thumb/Contact Us.png" width="150" height="23" /></li>
<li class="services"><img src="images/gallery/thumb/Services.png" width="113" height="24" /></li>
</ul>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑
也许使用数据属性而不是类名更好:
这样您可以更改类或添加类而不破坏它。
EDIT
Perhaps it is even better to use the data attribute instead of the classname:
This way you can change the classes or add classes without breaking it.
所以在这里,我假设 lis 的类名直接与您想要隐藏/显示的 div 的 id 相关。
因此,第一个函数(mouseover)获取您鼠标悬停的 li 的类名,然后将其添加到前缀
div#display_
中,以构建您想要显示的 div 的 id,然后淡入该元素。mouseout 函数只是隐藏所有的 div。如果动画已进入淡入状态,则
stop()
会停止动画。希望这会有所帮助。
So here, I've assumed that the class names of the lis directly relate the ids of the divs you want to hide/show.
The first function (mouseover) therefore gets the classname of the li you've moused over, it then adds it to the prefix
div#display_
in order to build the id of the div you want to show, and then fades in that element.The mouseout function just hides all the divs. The
stop()
stops the animation, if its part way through fading in.Hope this helps.
这是最简单的版本,假设内容按相同顺序排列,无需返回脚本并进行编辑。
This is the simplest version supposing the content is in the same order, without a need to come back to your script and edit.