简体   繁体   English

带有设置链接的背景位置的jQuery数组

[英]jquery array of background positions with set links

Okay I've been working on this fiddle . 好吧,我一直在研究这个小提琴 Basically it is four buttons which control the background image position but going from position to position is isn't smooth. 基本上是四个按钮来控制背景图像的位置,但是从一个位置移动到另一个位置并不平滑。 Would there be a better way of doing the samething? 会有更好的方法做同样的事情吗?

Here is my Code: 这是我的代码:

html: 的HTML:

<div id="container">
    <div id="content">
        <ul>
            <li id="btn-0">button-1</li>
            <li id="btn-1">button-2</li>
            <li id="btn-2">button-3</li>
            <li id="btn-3">button-4</li>
        </ul>
    </div>
</div>

Javascript: Javascript:

$(document).ready(function () {
    $('li').click(function () {
        currClick = $(this).index();
        bgPos = [{
            'background-position-x': '0px',
                'background-position-y': '0px'
        }, // 0
        {
            'background-position-x': '-50px',
                'background-position-y': '-150px'
        }, // 1
        {
            'background-position-x': '150px',
                'background-position-y': '-20px'
        }, // 2
        {
            'background-position-x': '-200px',
                'background-position-y': '-250px'
        } // 3
        ];
        $('#container').animate(bgPos[currClick], 1500, 'swing');
    });
});

CSS: CSS:

#container {
    height:500px;
    width:500px;
    background-image:url("http://postmediawindsorstar.files.wordpress.com/2013/01/cat_shot_by_arrow-1.jpg");
}
#content {
    position:absolute;
    margin-left:-250px;
    left:50%;
    height:300px;
    width:300px;
    border:1px solid #FFF;
}
li {
    color:#FFF;
}

Also if you spam click the buttons, it does not stop its current animation before moving to the next position. 同样,如果您点击了垃圾邮件按钮,则它不会在移动到下一个位置之前停止其当前动画。

Try using; 尝试使用;

$('#container').clearQueue();
$('#container').stop();
$('#container').animate(bgPos[currClick], 1500, 'swing');

CSS Animations are much quicker than js in the browser. CSS动画在浏览器中比js快得多。 I would look into them: 我会调查他们:

http://css3.bradshawenterprises.com/blog/jquery-vs-css3-transitions/ http://css3.bradshawenterprises.com/blog/jquery-vs-css3-transitions/

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

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