JavaScript YUI newsticker

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript YUI newsticker相关的知识,希望对你有一定的参考价值。

HTML:
<div class="newsTicker">
 <ul>
  <li>
   <span>14-11-2008</span> <a href="#">Student Informatica wint eerste prijs waterschapsdebat</a>
  </li>

  <li>
   <span>14-11-2008</span> <a href="#">Basisschoolleerlingen bewust van respect</a>
  </li>

  <li>
   <span>14-11-2008</span> <a href="#">Tijdgebrek groot probleem voor studenten met functiebeperking</a>
  </li>

  <li>
   <span>14-11-2008</span> <a href="#">Regisseur Matanic wint Stenden Media Student Award</a>
  </li>

  <li>
   <span>14-11-2008</span> <a href="#">Stenden-studenten onderweg naar Dakar voor KiKa</a>
  </li>
 </ul>
</div>

CSS:
div.newsTicker {
 position: relative;
 width: 400px;
 overflow: hidden;
}
div.newsTicker ul li {
 white-space: nowrap;
 float: left;
 padding-right: 30px; /* don't change to margin = margin between news items */
}


JAVASCRIPT:
var Dom = YAHOO.util.Dom;
var Event = YAHOO.util.Event;

YAHOO.namespace("snippet");
YAHOO.snippet.ticker = {
	init : function(className){
		this.speed = 150;
		// get all ticker element from a page
		this.tickerEl = Dom.getElementsByClassName(className);
		for(var i=0; i<this.tickerEl.length; i++) {
			this.buildTicker(this.tickerEl[i]);
		}
	},
	buildTicker : function(tickerEl) {
		// get all newsitems from a ticker element
		this.tickerItem = tickerEl.getElementsByTagName("li");
		this.tickerItemContainer = tickerEl.getElementsByTagName("ul");

		// get width of all list items and set the container to this width
		this.containerWidth = 0;
		for(var i=0; i<this.tickerItem.length; i++) {
			this.containerWidth += Dom.getRegion(this.tickerItem[i]).right - Dom.getRegion(this.tickerItem[i]).left;
		}
		Dom.setStyle(this.tickerItemContainer[0],"width",this.containerWidth + "px");

		// set position of container to the left of the containing box
		Dom.setStyle(this.tickerItemContainer[0],"left", (Dom.getRegion(tickerEl).right - Dom.getRegion(tickerEl).left) + "px");
		this.yPos = Dom.getRegion(this.tickerItemContainer[0]).top;

		// set listener for mouseover
		Event.addListener(this.tickerItemContainer[0],"mouseover",this.pauseAnim,this);

		// set listener for mouseout
		Event.addListener(this.tickerItemContainer[0],"mouseout",this.restartAnim,this);

		// start animation
		this.startAnim(tickerEl);
	},
	startAnim : function(tickerEl) {
		this.startPos = Dom.getStyle(this.tickerItemContainer[0],"left");
		this.startPos = this.startPos.split("px")[0];
		this.endPos = Dom.getRegion(tickerEl).left - this.containerWidth;

		this.attributes = {
			points: { to: [this.endPos,this.yPos] }
		};

		this.anim = new YAHOO.util.Motion(this.tickerItemContainer[0], this.attributes);
		this.currentWidth = this.containerWidth + parseFloat(this.startPos);
		this.anim.duration = this.currentWidth/this.speed;
		this.anim.useSeconds = true;
		this.anim.onComplete.subscribe(this.endAnim,this);
		this.anim.animate();
	},
	pauseAnim : function(e,obj) {
		obj.anim.stop();
	},
	restartAnim : function(e,obj) {
		obj.currentWidth = obj.containerWidth + Dom.getRegion(obj.tickerItemContainer[0]).left;
		obj.anim.duration = obj.currentWidth/obj.speed;
		obj.anim.animate();
	},
	endAnim : function(state,dur,obj) {
		if(Dom.getRegion(obj.tickerItemContainer[0]).left <= obj.endPos) {
			YAHOO.snippet.ticker.init("newsTicker");
		};
	}
}

initPage = function() {
	YAHOO.snippet.ticker.init("newsTicker");
}

Event.on(window,"load",initPage);

以上是关于JavaScript YUI newsticker的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript 使用YUI javascript库切换/显示

JavaScript 手风琴与YUI javascript库

JavaScript YUI配置实用程序指南

如何在 Ant 构建脚本中为 javascript 和 css 使用 YUI Compressor

YUI-确定DOM何时可以使用

在一个应用程序中将 jQuery 和 YUI 混合在一起,这很容易吗?