有关如何对列表项的背景进行动画处理的建议
Advice about how to animate the background of a list item
我想知道你是否可以为我提供一个更好的方法来实现我在这个小提琴中创建的效果:http://jsfiddle.net/YLuKh/1/
基本上,我想对锚标签的背景颜色进行动画处理,以显示图像,我已经通过将锚标签放置在图像顶部的跨度顶部,然后在悬停时对跨度的宽度进行动画处理。谁能提出更直接的方法
?.HTML
<ul id="test">
<li>
<a href="">This is the link</a>
<span class="bg"></span>
<img src="http://www.ritaxxii.org/wp-content/uploads/Luxury-Bedroom-Furniture-1.jpg" />
</li>
</ul>
.JS
$(document).ready(function() {
var li_width = $('#test').find('li').width();
console.log(li_width);
$('#test').find('li').on('mouseover', function() {
$(this).find('.bg').stop().animate({
width: '0'
}, 200);
}).on('mouseout', function() {
$(this).find('.bg').stop().animate({
width: li_width
}, 200);
});
});
正如我在评论中提到的,您可以使用背景位置来制作动画。这是一个仅使用背景图像定位的简单方法(http://jsfiddle.net/3PESX/(
$('a').mouseenter(function() {
$(this).stop().animate({ 'background-position-x': '-700px'}, 300);
});
$('a').mouseleave(function() {
$(this).stop().animate({ 'background-position-x': '0'}, 300);
});
a {
display: inline-block;
height: 50px;
width: 300px;
background: transparent url(http://jtrujillo.net/digital-photo-tutorials/8vs16bit/dgr1.jpg) 0 top no-repeat;
color: grey;
text-decoration: none;
line-height: 50px;
}
<a href="/">This is a link text</a>
请注意,背景位置属性是 x 和 y 版本的组合。不能对复合属性进行动画处理,需要分别对 X 和 Y 版本进行动画处理。或者,您可以使用 css 钩子插件来实现它。你可以在这里找到这些: https://github.com/brandonaaron/jquery-cssHooks
你可以从这里得到一个参考: http://snook.ca/archives/javascript/jquery-bg-image-animations
我可以建议一种仅限 CSS3 的方法来实现我认为您要做的事情:
li {
border: 1px solid #f90;
width: 504px; /* width of the image, adjust to taste */
overflow: hidden;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-o-box-sizing: border-box;
box-sizing: border-box;
}
li a {
display: block;
position: relative;
width: 100%;
height: 2em;
line-height: 2em;
color: #fff;
background-color: #000;
-webkit-transition: width 1s linear;
-moz-transition: width 1s linear;
-o-transition: width 1s linear;
-ms-transition: width 1s linear;
transition: width 1s linear;
}
li:hover a {
width: 0;
-webkit-transition: width 1s linear;
}
li a::after {
content: url(http://www.ritaxxii.org/wp-content/uploads/Luxury-Bedroom-Furniture-1.jpg);
position: absolute;
top: 0;
right: 0;
left: 100%;
bottom: 0;
}
JS小提琴演示。
如果你要有很多列表项,你可能需要考虑对 #test 元素的事件委托,这样你就不必将一堆不同的事件侦听器附加到每个 li 标签
//attach one event listener for 'mouseover' and one for 'mouseout' on the test element
$('#test').on('mouseover', 'li', function(){
//'this' is still the li element
console.log( $(this));
$(this).find('.bg').stop().animate({width: '0'},200);
}).on('mouseout', 'li', function(){
$(this).find('.bg').stop().animate({width: li_width},200);
});
相关文章:
- 使用不同的超时对数组中的每个项目进行角度动画处理
- 图表.js 2,从右到左(不是自上而下)进行动画处理
- 使用 JavaScript 对 HTML5 进度条进行动画处理
- 使用 JQuery 对 Spin SVG 元素进行动画处理
- 使用 GSAP 对模糊滤镜进行动画处理
- 使用 jQuery 对窗口进行动画处理
- 如何使用jQuery对固定元素的位置进行动画处理
- jQuery 动画处理最大宽度溢出问题
- 悬停不起作用时对不透明度更改进行动画处理
- 在两个值之间插值或“补间”(但不进行动画处理)
- 尝试在画布中对形状进行动画处理时,它会显示,但不移动
- 如何在不分隔跨度字母的情况下对文本进行动画处理
- 对链接的 svg 进行动画处理不起作用;内联时工作正常
- 防止对某些事件进行进一步的动画处理
- Snap.svg:在动画中使用相同的种类对多个零件进行动画处理
- 如何使用if语句jquery对数字进行动画处理
- 按随机顺序对元素进行动画处理
- 元素在使用 jQuery 对相对包装宽度进行动画处理时消失
- brush.event 在对画笔事件进行动画处理时会做什么
- 使用ScrollMagic按顺序对多个场景进行动画处理