如何修改此循环? [英] How to modify this loop?

查看:61
本文介绍了如何修改此循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



<前lang =Javascript> var scrollSpeed = 50 < /跨度>; // 以毫秒为单位的速度
var step = 1 ; // 每步移动多少像素
var current = 0 ; // 当前像素行
var imageHeight = 393 ; // 背景图片高度
var headerHeight = 300 ; // 标题有多高。

// 像素行开始新循环的位置
var restartPosition = - (imageHeight - headerHeight);

function scrollBg(){

// 转到下一个像素行。
current - = step;

// 如果在图片的末尾,则转到顶部。
if (current == restartPosition){
current = 0 ;
}

// 设置标题的CSS。
$(' #header')。css( background-position 0 +当前+ px);

}
// 重复调用滚动功能
var init = setInterval( scrollBg() ,scrollSpeed);



在此代码中,间隔是不断重复但我希望它停在第一个间隔结束的位置。

如果您有任何人可以修改此循环,请执行。



感谢

解决方案

' #header')。css( background-position 0 + current + px);

}
// 重复调用滚动功能
var init = setInterval( scrollBg() ,scrollSpeed);



在此代码中,间隔是不断重复但我希望它停在第一个间隔结束的位置。

如果您有任何人可以修改此循环,请执行。



谢谢


请参阅,



下面的代码行只重复调用函数,在条件内执行这行代码,或者如果你只想执行一次只需注释这行代码。 />


  //  调用重复滚动功能 
var init = setInterval( scrollBg(),scrollSpeed);


我认为你必须调用clearInterval函数。



这里的用法示例:



http://www.w3schools.com/jsref/met_win_clearinterval.asp [ ^ ]


Hi,

var scrollSpeed = 50; 		// Speed in milliseconds
var step = 1; 			// How many pixels to move per step		    
var current = 0;		// The current pixel row
var imageHeight = 393;		// Background image height
var headerHeight = 300;		// How tall the header is.
			
//The pixel row where to start a new loop
var restartPosition = -(imageHeight - headerHeight);
			
function scrollBg(){
				
	//Go to next pixel row.
	current -= step;
				
	//If at the end of the image, then go to the top.
	if (current == restartPosition){
			current = 0;
		}
			
		//Set the CSS of the header.
		$('#header').css("background-position","0 "+current+"px");
				
		}
                      //Calls the scrolling function repeatedly
			var init = setInterval("scrollBg()", scrollSpeed);


In this code the interval is keep repeating but what i want it to stop at the position where 1st interval end.
If any of you can modify this loop then please do.

thanks

解决方案

('#header').css("background-position","0 "+current+"px"); } //Calls the scrolling function repeatedly var init = setInterval("scrollBg()", scrollSpeed);


In this code the interval is keep repeating but what i want it to stop at the position where 1st interval end.
If any of you can modify this loop then please do.

thanks


See,

the below line of code only calling the function repeatedly, execute this line of code inside of a condition or if you want to execute only one time just comment this line of code.

//Calls the scrolling function repeatedly
var init = setInterval("scrollBg()", scrollSpeed);


I think you must call clearInterval function.

Example usage here:

http://www.w3schools.com/jsref/met_win_clearinterval.asp[^]


这篇关于如何修改此循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆