我已经编写了一些代码,允许我使用 css3 在单击时为背景图像的移动添加过渡。我的代码说“单击时将 874px 添加到背景位置的 x 值”。在我在项目上添加我的过渡之前,这非常有效,因为它允许用户在过渡期间单击,这会破坏我的场景。我只希望用户查看 874px 的倍数。有没有更好的方法可以做到这一点而不是超时?我希望用户能够多次单击,但只能达到最接近的 874px 倍数。
希望这有点道理。感谢您的任何帮助。JSFIDDLE 演示
HTML
<div class="container">
<div class="decoration two"></div>
<div class="decoration one"></div>
<span id="btnPrev" class="prev">prev</span>
<span id="btnNext" class="next">next</span>
</div>
JS
var btnPrev = $("#btnPrev");
var btnNext = $("#btnNext");
var decorationOne =$(".decoration.one");
var decorationTwo =$(".decoration.two");
$(function () {
btnNext.click(function() {
var DOG = decorationOne.css('background-position');
var splat = DOG.replace(/[^-\d\. ]/g, '').split(" ");
var new_values = [parseFloat(splat[0]) - 874 + "px", parseFloat(splat[1]) + 0 +"px"]; // x, y
decorationOne.css('background-position', new_values.join(" "));
});
btnPrev.click(function() {
var DOG = decorationOne.css('background-position');
var splat = DOG.replace(/[^-\d\. ]/g, '').split(" ");
var new_values = [parseFloat(splat[0]) + 874 + "px", parseFloat(splat[1]) + 0 +"px"]; // x, y
decorationOne.css('background-position', new_values.join(" "));
});
});
CSS
.container {
background: none repeat scroll 0 0 #E7F3FF;
border: 4px solid blue;
border-radius: 16px 16px 16px 16px;
box-shadow: 0 2px 2px rgba(1, 1, 1, 0.2);
color: #054B98;
height: 400px;
margin: 35px 0 15px;
overflow: hidden;
position: relative;
width: 874px;
}
.decoration {
position: absolute;
}
.decoration.one {
background: url("http://s22.postimg.org/ljly8r7tr/hills_1.png") repeat scroll left 0 transparent;
bottom: 0;
height: 500px;
-webkit-transition: background-position 1s ease 0s;
-moz-transition: background-position 1s ease 0s;
transition: background-position 1s ease 0s;
width: 2622px;
}
.decoration.two {
background: blue;
bottom: -180px;
height: 472px;
width: 5760px;
}
.prev {
position: absolute;
top: 50%;
left: 20px;
}
.next {
position: absolute;
top: 50%;
right: 20px;
}