Code //找出所有具有相同类名的元素 function hasClass(name, type) { var r = []; var re =new RegExp("(^|\\s)"+ name +"(\\s|$)"); var e = document.getElementsByTagName(type ||"*"); for (var j =0; j < e.length; j++) if (re.test(e[j].className))
r.push(e[j]); return r;
};
Code // 实现渐隐或者渐显动画的函数
// 参数:动画元素,开始透明度,结束透明度,动画持续时间
// 返回:动画句柄数组 function fade(elem, startVal, finishVal, time) { var diff = finishVal - startVal; var t = time ||1000; var handles = []; for (var i =0; i <100; i++) {
(function(n) {
handles.push( setTimeout(function() {
setOpacity(elem, (startVal + diff * n /100));
}, t * n /100)); })(i);
} return handles;
}
slide函数
Code // 实现滑动动画的函数,可以实现横/纵向滑动展开/收拢,或者横/纵向滑动位移
// 参数:动画元素,动画属性,开始值,结束值,动画持续时间
// 动画属性可选值:'top', 'left', 'height', 'left', 'padding', 'margin'
// 返回:动画句柄数组 function slide(elem, styleName, startVal, finishVal, time) { var diff = finishVal - startVal; var t = time ||1000; var handles = []; for (var i =0; i <200; i++) {
(function(n) {
handles.push( setTimeout(function() {
elem.style[styleName] = (startVal + (diff * n /200)) +'px';
}, t * n /200));
})(i);
} return handles;
}
一系列获取当前页面或窗口相关长宽和位置的并具有兼容性的函数
Code // 获取页面宽度 function pageWidth() { return document.body.scrollWidth;
}
// 获取页面高度 function pageHeight() { return document.body.scrollHeight;
}
// 获取浏览器视口高度 function windowHeight() { var de = document.documentElement; return self.innerHeight || (de && de.clientHeight) || document.body.clientHeight;
}
// 获取浏览器视口宽度 function windowWidth() { var de = document.documentElement; return self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
}
// 获取当前向右卷动的长度 function scrollX() { var de = document.documentElement; return self.pageXOffset || (de && de.scrollLeft) || document.body.scrollLeft;
}
// 获取当前像下卷动的长度 function scrollY() { var de = document.documentElement; return self.pageYOffset || (de && de.scrollTop) || document.body.scrollTop;
}
Code var ol = document.getElementById('DDoverlay'); var box = document.getElementById('DDimgBox'); var im = pb.getElementsByTagName('img')[0]; var note = pb.getElementsByTagName('div')[1]; var mg =50;
var elems = hasClass('DDbox', 'a'); for (var i =0; i < elems.length; i++) {
elems[i].onclick = showDDbox;
}
//找出所有具有相同类名的元素 function hasClass(name, type) { var r = []; var re =new RegExp("(^|\\s)"+ name +"(\\s|$)"); var e = document.getElementsByTagName(type ||"*"); for (var j =0; j < e.length; j++) if (re.test(e[j].className))
r.push(e[j]); return r;
};
function stopBubble(e) { // 如果传入了事件对象,那么就是非IE浏览器 if (e && e.stopPropagation) // 支持W3C的stopPropation()方法 e.stopPropagation(); else // 否则,我们得使用IE的方式来取消事件冒泡 window.event.cancelBubble =true;
}
function stopDefault(e) { if (e && e.preventDefault)
e.preventDefault(); else
window.event.returnValue =false; returnfalse;
}
function setOpacity(elem, level) { //注意这里两个filter的单复数形式 if (elem.filters)
elem.style.filter ='alpha(opacity='+ level +')'; else
elem.style.opacity = level /100;
}
function fade(elem, startVal, finishVal, time) { var diff = finishVal - startVal; var t = time ||1000; var handles = []; for (var i =0; i <100; i++) {
(function(n) {
handles.push( setTimeout(function() {
setOpacity(elem, (startVal + diff * n /100));
}, t * n /100)); })(i);
} return handles;
}
function slide(elem, styleName, startVal, finishVal, time) { var diff = finishVal - startVal; var t = time ||1000; var handles = []; for (var i =0; i <200; i++) {
(function(n) {
handles.push( setTimeout(function() {
elem.style[styleName] = (startVal + (diff * n /200)) +'px';
}, t * n /200));
})(i);
} return handles;
}
// 获取页面宽度 function pageWidth() { return document.body.scrollWidth;
}
// 获取页面高度 function pageHeight() { return document.body.scrollHeight;
}
// 获取浏览器视口高度 function windowHeight() { var de = document.documentElement; return self.innerHeight || (de && de.clientHeight) || document.body.clientHeight;
}
// 获取浏览器视口宽度 function windowWidth() { var de = document.documentElement; return self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
}
// 获取当前向右卷动的长度 function scrollX() { var de = document.documentElement; return self.pageXOffset || (de && de.scrollLeft) || document.body.scrollLeft;
}
// 获取当前像下卷动的长度 function scrollY() { var de = document.documentElement; return self.pageYOffset || (de && de.scrollTop) || document.body.scrollTop;
}
// addEvent/removeEvent written by Dean Edwards, 2005
// with input from Tino Zijdel
// http://dean.edwards.name/weblog/2005/10/add-event/ function addEvent(element, type, handler) { // assign each event handler a unique ID if (!handler.$$guid) handler.$$guid = addEvent.guid++; // create a hash table of event types for the element if (!element.events) element.events = {}; // create a hash table of event handlers for each element/event pair var handlers = element.events[type]; if (!handlers) {
handlers = element.events[type] = {}; // store the existing event handler (if there is one) if (element["on"+ type]) {
handlers[0] = element["on"+ type];
}
} // store the event handler in the hash table handlers[handler.$$guid] = handler; // assign a global event handler to do all the work element["on"+ type] = handleEvent;
}; // a counter used to create unique IDs addEvent.guid =1;
function removeEvent(element, type, handler) { // delete the event handler from the hash table if (element.events && element.events[type]) { delete element.events[type][handler.$$guid];
}
};
function handleEvent(event) { var returnValue =true; // grab the event object (IE uses a global event object) event = event || fixEvent(window.event); // get a reference to the hash table of event handlers var handlers =this.events[event.type]; // execute each event handler for (var i in handlers) { this.$$handleEvent = handlers[i]; if (this.$$handleEvent(event) ===false) {
returnValue =false;
}
} return returnValue;
};
function domReady( f ) { // If the DOM is already loaded, execute the function right away if ( domReady.done ) return f();
// If we’ve already added a function if ( domReady.timer ) { // Add it to the list of functions to execute domReady.ready.push( f );
} else { // Attach an event for when the page finishes loading, // just in case it finishes first. Uses addEvent. addEvent( window, "load", isDOMReady );
// Initialize the array of functions to execute domReady.ready = [ f ];
// Check to see if the DOM is ready as quickly as possible domReady.timer = setInterval( isDOMReady, 13 );
}
}
// Checks to see if the DOM is ready for navigation function isDOMReady() { // If we already figured out that the page is ready, ignore if ( domReady.done ) returnfalse;
// Check to see if a number of functions and elements are // able to be accessed if ( document && document.getElementsByTagName &&
document.getElementById && document.body ) {
// If they’re ready, we can stop checking clearInterval( domReady.timer );
domReady.timer =null;
// Execute all the functions that were waiting for ( var i =0; i < domReady.ready.length; i++ )
domReady.ready[i]();
// Remember that we’re now done domReady.ready =null;
domReady.done =true;
}
}
DDbox.js
addEvent(window, 'load', runDDbox);
function runDDbox() {
createBox(); var ol = document.getElementById('DDoverlay'); var box = document.getElementById('DDimgBox'); var im = pb.getElementsByTagName('img')[0]; var note = pb.getElementsByTagName('div')[1]; var mg =50;
var elems = hasClass('DDbox', 'a'); for (var i =0; i < elems.length; i++) {
elems[i].onclick = showDDbox;
}