简体   繁体   English

如何在Jquery中更改图片的位置?

[英]How can i change the position of an image in Jquery?

I have an image like this: 我有这样的图像:

在此处输入图片说明

and i want to change the position in jquery on hover to make it look the the image is changing. 我想在悬停时更改jquery中的位置,以使其看起来图像在变化。

I dont want to use the .animate function to shift the image but, to basically have the image scroll to each frame. 我不想使用.animate函数来移动图像,但是,基本上要使图像滚动到每一帧。

right now i have a width set, and my overflow to hidden so you can only see one image at a time. 现在,我设置了宽度,而我的溢出隐藏了,因此一次只能看到一张图像。

my HTML 我的HTML

 <ul class="icons">
                    <li id="trasklogo"><img src="img/icons/nav-se1aec3ccea.png" width="75" height="823" /></li>
                    <li><img src="img/icons/sprite_about.png" width="1050" height="70" /></li>
                    <li><img src="img/icons/sprite_genetics.png" width="1050" height="70" /></li>
                    <li><img src="img/icons/sprite_innovation.png" width="1050" height="70" /></li>
                    <li><img src="img/icons/sprite_media.png" width="1050" height="70" /></li>
                </ul>

my CSS 我的CSS

ul li, {
list-style: none;
}

li {
display: list-item;
text-align: -webkit-match-parent;
}
.menu .icons #trasklogo {
height: 87px;
overflow: hidden;
}
.container .menu .icons {
width: 75px;
overflow: hidden;
}

It's not entirely clear what you are trying to accomplish. 尚不清楚您要完成什么。

If you are trying to make this sprite an animation, take a look at Spritely . 如果您要使此精灵成为动画,请查看Spritely This javascript will automatically move the sprite to emulate keyframe animation. 此javascript将自动移动精灵以模拟关键帧动画。 You simply supply a width and how quickly the sprite should move and it will do the rest. 您只需要提供一个宽度,以及精灵应该移动多快的时间,其余的就可以完成。

If you are trying to slowly pan across the image and stop, your best bet would be to use animate on the background x position. 如果你想在图像慢慢平移和停止,你最好的选择对的背景x位置使用动画。 I doubt this is your intention based on the sprite you've supplied and the fact that you requested not to use animate. 我怀疑这是基于您提供的Sprite以及您请求不使用动画的事实而来。

You could also emulate this animation with CSS using @keyframes . 您也可以使用@keyframes用CSS模拟此动画。 See this fiddle: http://jsfiddle.net/ZLyrN/ 看到这个小提琴: http : //jsfiddle.net/ZLyrN/

Note: CSS animations are not cross-browser compatible without a polyfill . 注意:没有polyfill的 CSS动画不兼容跨浏览器。

You can easily create that effect in Javascript with JQuery. 您可以使用JQuery在Javascript中轻松创建该效果。

setInterval(function() {
    // parse the current margin to an integer and substract the with of one frame
    var nr=parseInt($("#sw").css("margin-left")) - 70; 
    // reset margin to 0 if margin > last frame
    if(nr<=-1050) {
        nr=0;
    }
    // set the new margin
    $("#sw").css("margin-left", nr+"px");
}, 50);

CSS: CSS:

#wr {
    width:70px;
    overflow:hidden;
}

HTML: HTML:

<div id="wr">
    <img id="sw" src="http://i.stack.imgur.com/hCX5s.png" />
</div>

Working example: http://jsfiddle.net/U2MrB/ 工作示例: http : //jsfiddle.net/U2MrB/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM