滑動代碼js,滾動的代碼

本文目錄一覽:

頁面實現滑動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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-16 14:15
下一篇 2024-11-16 14:15

相關推薦

  • JS Proxy(array)用法介紹

    JS Proxy(array)可以說是ES6中非常重要的一個特性,它可以代理一個數組,監聽數據變化並進行攔截、處理。在實際開發中,使用Proxy(array)可以方便地實現數據的監…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變數和數…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • 倉庫管理系統代碼設計Python

    這篇文章將詳細探討如何設計一個基於Python的倉庫管理系統。 一、基本需求 在著手設計之前,我們首先需要確定倉庫管理系統的基本需求。 我們可以將需求分為以下幾個方面: 1、庫存管…

    編程 2025-04-29
  • 寫代碼新手教程

    本文將從語言選擇、學習方法、編碼規範以及常見問題解答等多個方面,為編程新手提供實用、簡明的教程。 一、語言選擇 作為編程新手,選擇一門編程語言是很關鍵的一步。以下是幾個有代表性的編…

    編程 2025-04-29
  • Python實現簡易心形代碼

    在這個文章中,我們將會介紹如何用Python語言編寫一個非常簡單的代碼來生成一個心形圖案。我們將會從安裝Python開始介紹,逐步深入了解如何實現這一任務。 一、安裝Python …

    編程 2025-04-29
  • 怎麼寫不影響Python運行的長段代碼

    在Python編程的過程中,我們不可避免地需要編寫一些長段代碼,包括函數、類、複雜的控制語句等等。在編寫這些代碼時,我們需要考慮代碼可讀性、易用性以及對Python運行性能的影響。…

    編程 2025-04-29
  • 北化教務管理系統介紹及開發代碼示例

    本文將從多個方面對北化教務管理系統進行介紹及開發代碼示例,幫助開發者更好地理解和應用該系統。 一、項目介紹 北化教務管理系統是一款針對高校學生和教職工的綜合信息管理系統。系統實現的…

    編程 2025-04-29

發表回復

登錄後才能評論