jQuery 最接近();
我正在尝试对列表项内部分隐藏(通过溢出:隐藏)的图像进行动画处理。我希望当用户将鼠标悬停在同一列表项内的 A 标签上时发生这种情况。
我有以下标记:
<div id="projects" class="section">
<ul>
<li>
<img src="assets/img/projects/pf6.jpg" width="980" height="500" alt="Project title" />
<h2 class="left middle"><span>new</span><a href="#">Title 1</a></h2>
</li>
<li>
<img src="assets/img/projects/pf4.jpg" width="980" height="500" alt="Project title" />
<h2 class="bottom right"><a href="#">Title 2</a></h2>
</li>
</ul>
</div>
我的基本 css:
#projects ul li {
width: 100%;
height: 450px;
position: relative;
display: block;
margin-bottom: 20px;
color: #fff;
overflow: hidden;
}
#projects ul li img {
position: absolute;
top: -50px;
left: 0;
}
我正在尝试使用 jQuery 进行以下操作来移动图像(无济于事):
$("#projects li h2 a").hover(
function () {
$(this).closest("img").animate({paddingTop: "50px"}, "slow");
},
function () {
$(this).closest("img").animate({paddingTop: "0"}, "slow");
}
);
任何人都知道为什么这不起作用! - 非常感谢任何帮助:-)
I'm trying to animate an image which is partly hidden (via overflow: hidden) inside a list item. I want this to happen when a user hovers over an A tag inside the same list item.
I have the following markup:
<div id="projects" class="section">
<ul>
<li>
<img src="assets/img/projects/pf6.jpg" width="980" height="500" alt="Project title" />
<h2 class="left middle"><span>new</span><a href="#">Title 1</a></h2>
</li>
<li>
<img src="assets/img/projects/pf4.jpg" width="980" height="500" alt="Project title" />
<h2 class="bottom right"><a href="#">Title 2</a></h2>
</li>
</ul>
</div>
My basic css:
#projects ul li {
width: 100%;
height: 450px;
position: relative;
display: block;
margin-bottom: 20px;
color: #fff;
overflow: hidden;
}
#projects ul li img {
position: absolute;
top: -50px;
left: 0;
}
I am trying the following with jQuery to move the image (to no avail):
$("#projects li h2 a").hover(
function () {
$(this).closest("img").animate({paddingTop: "50px"}, "slow");
},
function () {
$(this).closest("img").animate({paddingTop: "0"}, "slow");
}
);
Anyone have any idea why this is not working! - any help much appreciated :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为应该是:
或者你可以这样做:
I think it should be:
Or you could do:
closest()
仅选择当前元素及其父元素(然后将其限制为第一个匹配项)。您的
img
元素不是悬停处理程序所在链接的父级,因此它不起作用。更新:正如 ScottyUSCD 指出的那样,我之前发布的代码将不起作用。我误读了来源并认为这些元素是兄弟姐妹。
他的答案是正确的:
这将导航到最近的父
li
元素,然后在该元素的子元素中查找任何img
元素。closest()
only selects the current element and its parent elements (and then limits it to the first match).Your
img
element isn't a parent of the link you have the hover handler on, therefore it doesn't work.Update: as ScottyUSCD pointed out, the previous code I posted won't work. I misread the source and thought the elements were siblings.
His answer was correct:
This will navigate up to the closest parent
li
element, then look through that element's children for anyimg
elements.如果
也可能位于
之后,请使用:
If the
<img>
could also be after the<h2>
, use: