本文目錄一覽:
頁面實現滑動JS代碼
js實現隨頁面滑動效果的方法。具體如下:
頁面向上向下滾動,分享到的模塊隨著滑動。
要點:
代碼如下:
var scrtop =document.documentElement.scrollTop||document.body.scrollTop;
var height = document.documentElement.clientHeight||document.body.clientHeight;
var top = scrtop + (height – jb51.offsetHeight)/2;
top = parseInt(top);
獲得頁面垂直居中的位置
上代碼:
!DOCTYPE html
html
head
meta charset=”gb2312″ /
title無標題文檔/title
style
body{margin:0; padding:0; font:12px/1.5 arial; height:2000px;}
#jb51{width:100px; height:200px; line-height:200px;
text-align:center; border:1p solid #ccc;
background:#f5f5f5; position:absolute; left:-100px; top:0;}
#jb51_tit{position:absolute; right:-20px; top:60px;
width:20px; height:60px; padding:10px 0;
background:#06c; text-align:center;
line-height:18px; color:#fff;}
/style
script
window.onload = function(){
var jb51 = document.getElementById(“jb51”);
jb51.onmouseover = function(){
startrun(jb51,0,”left”)
}
jb51.onmouseout = function(){
startrun(jb51,-100,”left”)
}
window.onscroll = window.onresize = function(){
var scrtop=document.documentElement.scrollTop||document.body.scrollTop;
var height=document.documentElement.clientHeight||document.body.clientHeight;
var top = scrtop + (height – jb51.offsetHeight)/2;
top = parseInt(top);
startrun(jb51,top,”top”)
}
}
var timer = null
function startrun(obj,target,direction){
clearInterval(timer);
timer = setInterval(function(){
var speed = 0;
if(direction == “left”){
speed = (target-obj.offsetLeft)/8;
speed = speed0?Math.ceil(speed):Math.floor(speed);
if(obj.offsetLeft == target){
clearInterval(timer);
}else{
obj.style.left = obj.offsetLeft + speed + “px”;
}
}
if(direction == “top”){
speed = (target-obj.offsetTop)/8;
speed = speed0?Math.ceil(speed):Math.floor(speed);
if(obj.offsetTop == target){
clearInterval(timer);
}else{
obj.style.top = obj.offsetTop + speed + “px”;
}
document.title = obj.offsetTop + ‘,’ + target + ‘,’ +speed;
}
},30)
}
/script
/head
body
div id=”jb51″
分享到內容
span id=”jb51_tit”分享到/span
/div
/body
/html
網頁js左右滑動代碼。
script
var show = 0;
function leftRun(){
show-=100;
document.getElementById(“b”).style.marginLeft = show+”px”;
}
function leftRun(){
show+=100;
document.getElementById(“b”).style.marginRight= show+”px”;
}
/script
div id=”a” width=”500″
div id=”b” width=”1000″
正文內容
/div
/div
input type=”button” id=”leftRun” onclick=”leftRun()” /
input type=”button” id=”rightRun” onclick=”rightRun()” /
js如何實現慣性滑動效果
主要思路是:滑鼠當前點到下一點直接間隔計算出速度。這樣就實現了慣性滑動效果。
下面是簡單的js代碼實現:僅供參考:
style
#div1{ width:100px; height:100px; background:red; position:absolute; left:0px; top:0;}
/style
script
window.onload=function(){
var oDiv=document.getElementById(‘div1’);
var iSpeedX=0;
var iSpeedY=0;
var lastX=0;
var lastY=0;
var timer=null;
oDiv.onmousedown=function(ev){ //div的滑鼠按下事件,主要計算滑鼠當前位置,和移動位置。這樣可以計算出滑鼠移動速度。
var oEvent=ev || event;
var disX=oEvent.clientX-oDiv.offsetLeft;
var disY=oEvent.clientY-oDiv.offsetTop;
clearInterval(timer);
document.onmousemove=function(ev){ //滑鼠拖動事件。
var oEvent=ev || event;
oDiv.style.left=oEvent.clientX-disX+’px’;
oDiv.style.top=oEvent.clientY-disY+’px’;
iSpeedX=oEvent.clientX-lastX;
iSpeedY=oEvent.clientY-lastY;
lastX=oEvent.clientX;
lastY=oEvent.clientY;
}
document.onmouseup=function(){ //當滑鼠抬起後,清掉移動事件。
document.onmousemove=null;
document.onmouseup=null;
oDiv.releaseCapture oDiv.releaseCapture();
startMove();
}
oDiv.setCapture oDiv.setCapture();
return false;
}
function startMove(){ //移動函數,主要操作是計算滑鼠移動速度和移動方向。
clearInterval(timer);
timer=setInterval(function(){
iSpeedY+=3;
var t=oDiv.offsetTop+iSpeedY;
var l=oDiv.offsetLeft+iSpeedX;
if(tdocument.documentElement.clientHeight-oDiv.offsetHeight){
t=document.documentElement.clientHeight-oDiv.offsetHeight;
iSpeedY*=-0.8;
iSpeedX*=0.8;
}
if(t0){
t=0;
iSpeedY*=-0.8;
iSpeedX*=0.8;
}
if(ldocument.documentElement.clientWidth-oDiv.offsetWidth){
l=document.documentElement.clientWidth-oDiv.offsetWidth;
iSpeedX*=-0.8;
iSpeedY*=0.8;
}
if(l0){
l=0;
iSpeedX*=-0.8;
iSpeedY*=0.8;
}
oDiv.style.left=l+’px’;
oDiv.style.top=t+’px’;
if(Math.abs(iSpeedX)1)iSpeedX=0;
if(Math.abs(iSpeedY)1)iSpeedY=0;
if(iSpeedX==0 iSpeedY==0 t==document.documentElement.clientHeight-oDiv.offsetHeight){
clearInterval(timer);
}
document.title=i++;
},30);
}
};
/script
/head
body
div id=”div1″/div
/body
HTML5 滑動條js代碼怎麼寫?
強大的HTML5提供了一種新的標籤progress,只需要通過該標籤+簡單的js,即可以實現進度條滾動的效果。
代碼如下:
HTML
1
progress max=”100″ value=”0″ id=”pg”/progress
這個標籤意義很明確,並且屬性只有以上兩個,max表示需要完成的任務量,value是目前完成的任務量。
js
12345
var pg=document.getElementById(‘pg’); setInterval(function(e){ if(pg.value!=100) pg.value++; else pg.value=0;},100);
以上的實現方式不僅更加的語義化,同時也極大的簡化了我們的代碼,並且靈活性更高,所以熟練使用HTML5是非常有必要的!
不過HTML5標籤的瀏覽器兼容性也是我們需要考慮的一個問題,所以如果網頁需求的兼容性比較高的話,特別是對低版本IE有兼容需求的話,那麼HTML5的標籤就不太適用了。以下為progress的瀏覽器支持情況。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/154842.html