1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- /**
- * 获取滚动的头部距离和左边距离
- * scroll().top scroll().left
- * @returns {*}
- */
- function scroll() {
- if(window.pageYOffset !== null){
- return {
- top: window.pageYOffset,
- left: window.pageXOffset
- }
- }else if(document.compatMode === "CSS1Compat"){ // W3C
- return {
- top: document.documentElement.scrollTop,
- left: document.documentElement.scrollLeft
- }
- }
- return {
- top: document.body.scrollTop,
- left: document.body.scrollLeft
- }
- }
- function $(id) {
- return typeof id === "string" ? document.getElementById(id) : null;
- }
- /**
- * 获取屏幕的宽度和高度
- * @returns {*}
- */
- function client() {
- if(window.innerWidth){ // ie9+ 最新的浏览器
- return {
- width: window.innerWidth,
- height: window.innerHeight
- }
- }else if(document.compatMode === "CSS1Compat"){ // W3C
- return {
- width: document.documentElement.clientWidth,
- height: document.documentElement.clientHeight
- }
- }
- return {
- width: document.body.clientWidth,
- height: document.body.clientHeight
- }
- }
- /**
- * 匀速动画函数
- * @param {object}obj
- * @param {number}target
- * @param {number}speed
- */
- function constant(obj, target, speed) {
- // 1. 清除定时器
- clearInterval(obj.timer);
- // 2. 判断方向
- var dir = obj.offsetLeft < target ? speed : -speed;
- // 3. 设置定时器
- obj.timer = setInterval(function () {
- obj.style.left = obj.offsetLeft + dir + "px";
- if(Math.abs(target - obj.offsetLeft) < Math.abs(dir)){
- clearInterval(obj.timer);
- obj.style.left = target + "px";
- console.log(obj.offsetLeft, target);
- }
- }, 20);
- }
|