兼容主流浏览器的js原生函数封装

1.获取和修改垂直滚动条的滚动距离

    
//获取滚动条滚动距离
function getScrollTop(){ var fromTop=document.documentElement.scrollTop||document.body.scrollTop||window.pageYOffset; return fromTop; }
//修改滚动条滚动距离
function changeScroll(num){ if(document.documentElement.scrollTop){ document.documentElement.scrollTop+=num; } else if(document.body.scrollTop){ document.body.scrollTop+=num; } else{ window.pageYOffset+=num; } }

2.倒计时函数,需要创建一个id为countdown的div;

function countDown(time){      ///time为倒计时的秒
                var timer;
                var hours,minutes,seconds;
                timer=setInterval(function(){
                    if(time==0){
                        clearInterval(timer);
                    }
                    console.log();
                    hours=parseInt((time/60)/60);
                    minutes=parseInt((time/60)%60);
                    seconds=parseInt(time%60);
                    hours=hours>9?hours:"0"+hours;
                    minutes=minutes>9?minutes:"0"+minutes;
                    seconds=seconds>9?seconds:"0"+seconds;
                    document.getElementById("countdown").innerHTML=hours+":"+minutes+":"+seconds;
                    time--;
                },1000);
            }

 

 

持续更新中......

posted @ 2017-03-19 20:56  TateWang  阅读(253)  评论(0编辑  收藏  举报
Top