ITeye博客

通用滚动widget

bellstar · 2009-07-28 17:49 · javascript · Markdown


/**
 * 滚动HTML集合对象
 * @param {Element} container 容器节点
 * @param {Number} unitLength 滚动宽度,等于容器内子对象的宽度,子对象宽度必须一致
 * @param {Number} speed 每10毫秒移动的像素值
 * @param {String} direction 滚动方向(up, down, left, right)
 * @param {Number} containerRange 容器可视范围 width - paddingLeft - paddingRight 或者 height - paddingTop - paddingBottom
 */
Scroll = function(container, unitLength, speed, direction, containerRange){	
    var count = (function(){
        var qty = 0;
        for(var i=0,l=container.childNodes.length;i<l;i++)	{
            if(container.childNodes[i].nodeType==1)qty++;
        }	
        return qty;
    })();
	
	var container_length = (unitLength * count * 2);
	
	this.getContainer = function(){
		return container;
	}
	
    this.getCount = function(){return count;};
	
	/**
	*@param {String} direction 移动方向
	*@param {Number} perMoveCount 每次移动对象个数
	*/
	this.moveOnce = function(direction, perMoveCount, callback){
		if(isNaN(parseInt(perMoveCount)))perMoveCount = 1;
		var hash = {
			left: container.style.marginLeft,
			right: container.style.marginLeft,
			up: container.style.marginTop,
			down: container.style.marginTop
		}
		
		var v = parseInt(hash[direction]);
		var moved_length = Math.abs(v - init_pos[direction]);
		if(moved_length == unitLength * count)this.reset(direction);
		var remainder = count % perMoveCount;
		if(remainder){
			var full_times = Math.floor(count / perMoveCount) * perMoveCount;
			var thisObj = this;
			this.move(speed, full_times * unitLength, direction, function(){
				thisObj.move(speed, remainder * unitLength, direction, callback);		
			});			
		}else{
			this.move(speed, perMoveCount * unitLength, direction, callback);			
		}
	}
	
    this.init = function(){
        var childNodes = container.innerHTML;
        container.style.display = "block";
        container.innerHTML = childNodes + childNodes;
        if(direction == 'up' || direction == 'down'){
        	container.style.height = container_length + "px";
        }else if(direction == 'left' || direction == 'right'){
        	container.style.width = container_length + "px";
        }
		this.reset(direction);
    }

	this.scroll = function(){
		var thisObj = this;
		this.move(speed, unitLength * count, direction, function(){
			//console.info("scrolling");
			thisObj.reset(direction);
			thisObj.scroll(direction);
		});		
	}
	
	var init_pos = {
		left: 0,
		right: -(container_length - containerRange),
		up: 0,
		down: -(container_length - containerRange)
	}
	/**
	 *重置对象到初始位置
	 */
	this.reset = function(direction){
		//console.info("reset");
		if(direction == "left" || direction == "right"){
			container.style.marginLeft = init_pos[direction] + 'px';
		}else if(direction == "up" || direction == "down"){
			container.style.marginTop = init_pos[direction] + 'px';
		}
	}
}


/**
 * 移动对象
 * @private
 * @param {Element} moveObj 移动对象
 * @param {Number} speed 每10毫秒移动像素
 * @param {String} direction 移动方向
 * @param {Function} onMoved 移动后调用
 */
Scroll.prototype.move= function(speed, distance, direction, onMoved){
	var moveObj = this.getContainer();
    var i = Math.abs(distance);
	var scroll_px = speed;
    var handler = function(){
		if(direction == "left"){
			moveObj.style.marginLeft = parseInt(moveObj.style.marginLeft) - scroll_px + 'px';
		}else if(direction == "right"){
			moveObj.style.marginLeft = parseInt(moveObj.style.marginLeft) + scroll_px + 'px';
		}else if(direction == "up"){
			moveObj.style.marginTop = parseInt(moveObj.style.marginTop) - scroll_px + 'px';
		}else if(direction == "down"){
			moveObj.style.marginTop = parseInt(moveObj.style.marginTop) + scroll_px + 'px';
		}        
        i -= scroll_px;
		if(i > 0 && i < scroll_px){
			scroll_px = i;
		}
        if (i > 0) {
            window.setTimeout(handler, 10);
		}else{ // == 0
            if(typeof(onMoved) == "function")onMoved();
        }
    }
    window.setTimeout(handler, 10);
}

/*
----------------------单击按钮向右滚动示例--------------------------
<input id="btnScrollRight" name="" type="button" class="btn_goto"/>
<div style="width:636px;overflow:hidden;">
	<div id="scroll_prizes">
		<div class="list"><a href=""><img src="../imgaes/index/img1x.gif" alt='1' width="150" height="150" border="0" alt="" /></a></div>
		<div class="list"><a href=""><img src="../imgaes/index/img1x.gif" alt='2' width="150" height="150" border="0" alt="" /></a></div>
		<div class="list"><a href=""><img src="../imgaes/index/img1x.gif" alt='3' width="150" height="150" border="0" alt="" /></a></div>
		<div class="list"><a href=""><img src="../imgaes/index/img1x.gif" alt='4' width="150" height="150" border="0" alt="" /></a></div>
		<div class="list"><a href=""><img src="../imgaes/index/img1x.gif" alt='5' width="150" height="150" border="0" alt="" /></a></div>
		<div class="list"><a href=""><img src="../imgaes/index/img1x.gif" alt='6' width="150" height="150" border="0" alt="" /></a></div>
	</div>
</div>
<script>
	var mq1 = new Scroll(document.getElementById("scroll_prizes"), 159, 10, "right", 636, function(){});	
	mq1.init();
	//mq1.scroll();	
	var mq1_is_moving = false;
	document.getElementById("btnScrollRight").onclick = function(){
		//this.moveOnce = function(direction, perMoveCount, callback){
		if(mq1_is_moving)return;
		mq1_is_moving = true;
		mq1.moveOnce("right", 4, function(){
			mq1_is_moving = false;
		});
	}
</script>

---------------------循环向上滚动示例-------------------------------
<div class="co_list" style="height:566px;overflow:hidden;padding:20px 30px">
	<div style="height:536px;overflow:hidden;">
		<div id="marquee2">
			<a href=""><img src="../imgaes/index/co_adv11.png" width="150" height="55" border="0" alt=""/></a>
			<a href=""><img src="../imgaes/index/co_adv12.png" width="150" height="55" border="0" alt=""/></a>
			<a href=""><img src="../imgaes/index/co_adv13.png" width="150" height="55" border="0" alt=""/></a>
			<a href=""><img src="../imgaes/index/co_adv14.png" width="150" height="55" border="0" alt=""/></a>
			<a href=""><img src="../imgaes/index/co_adv15.png" width="150" height="55" border="0" alt=""/></a>
		</div>
	</div>
</div>
<script>	
	mq2 = new Scroll(document.getElementById("marquee2"), 69, 2, "up", 536, function(){});	
	mq2.init();
	mq2.scroll();
</script>

-------------------------循环向右滚动示例-------------------------
<div style="width:615px;overflow:hidden;height:110px;">
	<div id="marquee1">
		<div class="list">
			<img src="../imgaes/index/co_adv11.png" width="201" height="101" border="0" alt="" />
		</div>
		<div class="list">
			<img src="../imgaes/index/co_adv12.png" width="201" height="101" border="0" alt="" />
		</div>
		<div class="list">
			<img src="../imgaes/index/co_adv13.png" width="201" height="101" border="0" alt="" />
		</div>
		<div class="list">
			<img src="../imgaes/index/co_adv14.png" width="201" height="101" border="0" alt="" />
		</div>
		<div class="list">
			<img src="../imgaes/index/co_adv15.png" width="201" height="101" border="0" alt="" />
		</div>		
	</div>
</div>
<script>	
	var mq1 = new Scroll(document.getElementById("marquee1"), 211, 2, "right", 615, function(){});	
	mq1.init();
	mq1.scroll();
</script>	
*/

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。