【发布时间】:2011-09-16 20:12:23
【问题描述】:
抱歉,如果这具有误导性,我在 Java 脚本和 jQuery 方面还很陌生。谁能告诉我如何使导航链接在鼠标悬停时向左滑动并在鼠标悬停时滑回?我试图在鼠标悬停时单独滑动的 li 类用于此gallery image swap 脚本。我有一个带有图像的左侧导航平面,这些图像将在悬停时将相应的图像更改为右侧。
<script type>
$(document).ready(function() {
// Image swap on hover
$("#gallery ul li img").hover(function(){
$('#main-img').attr('src',$(this).attr('src').replace('thumb/',
'')).stop().hide().fadeTo("slow",1);
});
// Image preload
var imgSwap = [];
$("#gallery ul li img").each(function(){
imgUrl = this.src.replace('thumb/', '');
imgSwap.push(imgUrl);
});
$(imgSwap).preload();
});
$.fn.preload = function() {
this.each(function(){
$('<img/>')[0].src = this;
});
}
</script>
$("#gallery ul li img").hover(function(){ 中包含的 li 类是我想要在鼠标悬停时单独滑动并在鼠标悬停时返回的内容。我试过了
$("##gallery ul li img").hover(function(){
$("gallery ul.home", this).stop().animate({left:"150px"},{queue:false,duration:200});
}, function() {
$("gallery ul.home", this).stop().animate({right:"0px"},{queue:false,duration:200});
但这似乎打破了.hover(function(){
HTML:
<div id="gallery">
<img src="images/gallery/home.png" alt="" id="main-img" />
<ul>
<li class="home"><img src="images/gallery/thumb/home.png" alt="" /></li>
<li class="about"><img src="images/gallery/thumb/About Us.png" alt="" /></li>
<li class="contact"><img src="images/gallery/thumb/Contact Us.png" alt="" /></li>
<li class="services"><img src="images/gallery/thumb/Services.png" alt="" /></li>
</ul>
</div>
CSS:
#body #left_nav #gallery #main-img {
position: absolute;
left: 400px;
top: 20px;
right: auto;
bottom: auto;
}
#body #left_nav #links #gallery ul li{
display: inline;
clip: rect(auto,3px,auto,auto);
height: 500px;
width: 300px;
}
#body #left_nav #gallery ul .home {
position: absolute;
left: 117px;
top: 74px;
list-style-image: none;
list-style-type: none;
}
#body #left_nav #gallery ul .about {
position: absolute;
left: 88px;
top: 176px;
list-style-image: none;
list-style-type: none;
}
#body #left_nav #gallery ul .contact {
position: absolute;
left: 80px;
top: 277px;
list-style-image: none;
list-style-type: none;
}
#body #left_nav #gallery ul .services {
position: absolute;
top: 385px;
list-style-image: none;
list-style-type: none;
left: 98px;
}
【问题讨论】:
-
一个建议:当发布这样的内容时,请给我们一个关于 jsfiddle 的参考,这样可以更容易地测试大部分代码;)
-
你至少有一个语法错误我假设这只是一个错字? (您的悬停功能在最后一行缺少
});。
标签: jquery image hover slide swap