DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> 來自騰訊的一個不固定高度得消息的滾動特效
來自騰訊的一個不固定高度得消息的滾動特效
編輯:關於JavaScript     

[Ctrl+A 全選 注:如需引入外部Js需刷新才能執行]
看關鍵的js code:
復制代碼 代碼如下:
var $ = function (d){
typeof d == "string" &&(d = document.getElementById(d));
return $.fn.call(d);
};
$.fn = function (){
this.addEvent = function (sEventType,fnHandler){
if (this.addEventListener) {this.addEventListener(sEventType, fnHandler, false);}
else if (this.attachEvent) {this.attachEvent("on" + sEventType, fnHandler);}
else {this["on" + sEventType] = fnHandler;}
}
this.removeEvent = function (sEventType,fnHandler){
if (this.removeEventListener) {this.removeEventListener(sEventType, fnHandler, false);}
else if (this.detachEvent) {this.detachEvent("on" + sEventType, fnHandler);}
else { this["on" + sEventType] = null;}
}
return this;
};
var Class = {create: function() {return function() { this.initialize.apply(this, arguments); }}};
var Bind = function (obj,fun,arr){return function() {return fun.apply(obj,arr);}}
var Marquee = Class.create();
Marquee.prototype = {
initialize: function(id,name,out,speed) {
this.name = name;
this.box = $(id);
this.out = 3;//滾動間隔時間,單位秒
this.speed = speed;
this.d = 1;
this.box.style.position = "relative";
this.box.scrollTop = 0;
var _li = this.box.firstChild;
while(typeof(_li.tagName)=="undefined")_li = _li.nextSibling;
this.lis = this.box.getElementsByTagName(_li.tagName);
this.len = this.lis.length;
for(var i=0;i<this.lis.length;i++){
var __li = document.createElement(_li.tagName);
__li.innerHTML = this.lis[i].innerHTML;
this.box.appendChild(__li);//cloneNode
if(this.lis[i].offsetTop>=this.box.offsetHeight)break;
}
this.Start();
this.box.addEvent("mouseover",Bind(this,function(){clearTimeout(this.timeout);},[]));
this.box.addEvent("mouseout",Bind(this,this.Start,[]));
},
Start:function (){
clearTimeout(this.timeout);
this.timeout = setTimeout(this.name+".Up()",this.out*1000)
},
Up:function(){
clearInterval(this.interval);
this.interval = setInterval(this.name+".Fun()",10);
},
Fun:function (){
this.box.scrollTop+=this.speed;
if(this.lis[this.d].offsetTop <= this.box.scrollTop){
clearInterval(this.interval);
this.box.scrollTop = this.lis[this.d].offsetTop;
this.Start();
this.d++;
}
if(this.d >= this.len + 1){
this.d = 1;
this.box.scrollTop = 0;
}
}
};
$(window).addEvent("load",function (){
marquee = new Marquee("msg_weibo","marquee",1,2);
});

實現思路與以前的文字滾動是一樣的,都是先充滿當前容器,再通過scrollTop++往上滾的,只不過他是每次滾動的距離不是固定的,是取當前滾動消息的高度。由於scrollTop(滾出當前可視區域的高度)和offsetTop(距離父節點頂部的距離,常用於取某元素在頁面的坐標位置)的區別,所以通過 if(this.lis[this.d].offsetTop <= this.box.scrollTop)來判斷是否滾動完上條消息,需要停頓下了。

我覺得亮點之處在於$的寫法。通常Prototype裡也就取下obj||document.getElementById('objId'),他這裡除此外還幫obj綁定了一些方法。他的作用是不是類似於原型擴展String、Array等對象的方法呢。這個可以借鑒。
另外,他初始化時填充容器時用document.createElement->賦innerHTML->appendChild來做,我覺的不如直接cloneNode(true)->appendChild好,如不對,歡迎指正。

主要還是填下這個月的坑,哈哈。
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved