Javascript 关于如何设置列表项背景动画的建议

Javascript 关于如何设置列表项背景动画的建议,javascript,jquery,Javascript,Jquery,我想知道你能不能给我一个更好的方法来达到我在这把小提琴上创造的效果: 基本上,我想动画的背景颜色的锚标签显示一个图像,我已经做了定位锚标签顶部的跨度顶部的图像,然后在悬停动画宽度的跨度。有人能提出一个更直接的方法吗 HTML 你可以从这里得到一个参考:你可以从这里得到一个参考:我可以建议一个CSS3唯一的方法来实现我认为你想要做的事情: li { border: 1px solid #f90; width: 504px; /* width of the image, adjust

我想知道你能不能给我一个更好的方法来达到我在这把小提琴上创造的效果:

基本上,我想动画的背景颜色的锚标签显示一个图像,我已经做了定位锚标签顶部的跨度顶部的图像,然后在悬停动画宽度的跨度。有人能提出一个更直接的方法吗

HTML


你可以从这里得到一个参考:

你可以从这里得到一个参考:

我可以建议一个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;
}
​

.

我可以推荐一种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;
}
​

.

正如我在评论中提到的,您可以使用背景位置来制作动画。下面是一个仅使用背景图像定位的简单示例()

$('a').mouseenter(函数(){
$(this.stop().animate({'background-position-x':'-700px'},300);
});
$('a').mouseleave(函数(){
$(this.stop().animate({'background-position-x':'0'},300);
});​
a{
显示:内联块;
高度:50px;
宽度:300px;
背景:透明url(http://jtrujillo.net/digital-photo-tutorials/8vs16bit/dgr1.jpg)0顶部无重复;
颜色:灰色;
文字装饰:无;
线高:50px;
}​
​

请注意,“背景位置”属性是x和y版本的组合。不能设置复合属性的动画,需要分别设置X和Y版本的动画。或者,您可以使用css钩子插件使之成为可能。你可以在这里找到:

正如我在评论中提到的,你可以使用背景位置来制作动画。下面是一个仅使用背景图像定位的简单示例()

$('a').mouseenter(函数(){
$(this.stop().animate({'background-position-x':'-700px'},300);
});
$('a').mouseleave(函数(){
$(this.stop().animate({'background-position-x':'0'},300);
});​
a{
显示:内联块;
高度:50px;
宽度:300px;
背景:透明url(http://jtrujillo.net/digital-photo-tutorials/8vs16bit/dgr1.jpg)0顶部无重复;
颜色:灰色;
文字装饰:无;
线高:50px;
}​
​

请注意,“背景位置”属性是x和y版本的组合。不能设置复合属性的动画,需要分别设置X和Y版本的动画。或者,您可以使用css钩子插件使之成为可能。你可以在这里找到:

如果你有很多列表项,你可能想把事件委托考虑到一个测试元素,这样你就不必把一组不同的事件侦听器附加到每个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);       
});

如果你有很多列表项,你可能会想把事件委托考虑到你的测试元素,这样你就不必把一组不同的事件侦听器附加到每个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);       
});

你有没有尝试过只为锚定标签的css背景图像的位置设置动画?嘿,是的,我现在刚刚遇到了这个方法。你有没有试过只为锚定标记的css背景图像的位置设置动画?嘿,是的,我现在刚刚遇到了这个方法。我一定会试一试的
//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);       
});