当位置=高度Jquery时滚动背景图像以重置
我正在尝试生成一个jQuery动画,它滚动背景图像,直到它到达图像的末尾,然后再次开始 我已经设法让卷轴工作了&通过混合这里的各种代码来计算背景图像的高度,但是当我尝试将两者结合起来时,它就停止工作了 我不太熟悉javascript,所以我真的不知道发生了什么 代码如下:当位置=高度Jquery时滚动背景图像以重置,jquery,function,scroll,Jquery,Function,Scroll,我正在尝试生成一个jQuery动画,它滚动背景图像,直到它到达图像的末尾,然后再次开始 我已经设法让卷轴工作了&通过混合这里的各种代码来计算背景图像的高度,但是当我尝试将两者结合起来时,它就停止工作了 我不太熟悉javascript,所以我真的不知道发生了什么 代码如下: <div class="animateIcon"> <img src="images/spacer.png" width="144" height="167" class="animation" /&
<div class="animateIcon">
<img src="images/spacer.png" width="144" height="167" class="animation" />
</div>
jQuery
$(document).ready(function () {
var scrollSpeed = 100;
var current = 0;
var step = 167; // How many pixels to move per step
var direction = 'v';// set the direction
$.fn.getBgImage = function (callback) {
var height = 0;
var path = $(this).css('background-image').replace('url', '').replace('(', '').replace(')', '').replace('"', '').replace('"', '');
var tempImg = $('<img />');
tempImg.hide(); //hide image
tempImg.bind('load', callback);
$('body').append(tempImg); // add to DOM before </body>
tempImg.attr('src', path);
$('#tempImg').remove(); //remove from DOM
};
$(".animation").getBgImage(function () {
var restartPosition = -($(this).height());
});
function bgscroll() {
//Go to next pixel row.
current -= step;
//If at the end of the image, then go to the top.
if (current <= restartPosition) {
current = 0;
}
// move the background with backgrond-position css properties
$('.animation').css("background-position", (direction == 'v') ? "0 " + current + "px" : current + "px 0");
}
//Calls the scrolling function repeatedly
setInterval(bgscroll, scrollSpeed);
});
$(文档).ready(函数(){
速度=100;
无功电流=0;
var step=167;//每一步移动多少像素
var direction='v';//设置方向
$.fn.getBgImage=函数(回调){
var高度=0;
var path=$(this.css('background-image').replace('url','').replace('(','').replace('),'').replace(','');
var tempImg=$('
当我添加此代码时,它似乎会中断,但无法找出原因:
//If at the end of the image, then go to the top.
if (current <= restartPosition) {
current = 0;
}
//如果在图像的末尾,则转到顶部。
如果(当前我没有尝试运行代码,但看起来restartPosition
与bgcroll
不在同一范围内
尝试将var restartPosition;
与其他全局变量一起添加到顶部,并将var restartPosition=-($(this.height());
更改为restartPosition=-($(this.height());
谢谢,脚本现在可以工作了,但它不是获取背景图像的高度,而是获取浏览器的高度