资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

javascript滑动,js触摸滑动

页面实现滑动JS代码

js实现随页面滑动效果的方法。具体如下:

创新互联公司于2013年开始,是专业互联网技术服务公司,拥有项目网站建设、成都网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元慈利做网站,已为上家服务,为慈利各地企业和个人服务,联系电话:18982081108

页面向上向下滚动,分享到的模块随着滑动。

要点:

代码如下:

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如何实现惯性滑动效果

主要思路是:鼠标当前点到下一点直接间隔计算出速度。这样就实现了惯性滑动效果。

下面是简单的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

javascript的滑动学习 求每行的作用 没个function的作用 越详细越好 我是javascript新手

大哥,这么一大串的,都要求注释,太蛋疼了吧,这个代码估计浏览器兼容性有局限性,你是新手,还是用jquery吧,学习jquery用法,完成一样的功能估计少80%的代码,而且兼容各种浏览器。

大体讲下:

var glide =new function(){

//对象实列化

function $id(id){return document.getElementById(id);};

//里面的function就是这个类的方法,上面这个方法是知道Id,返回该id的dom对象

this.layerGlide=function(auto,oEventCont,oSlider,sSingleSize,second,fSpeed,point)

//这个对象的.layerGlide属性设置为方法,方法从上面的参数注释理解,应该控制滚动的

其他的基本什么可说,要做滚动控制层,主要是用js修改scrollTop,Left.....这些属性,比如往上面滚动,高度设置100px,超过部分css设置隐藏,scrollTop就是实际的层内容高度,js通过setInterval函数,多久时间执行js修改scrollTop的值+++,等内容全部滚玩了,又设置为0,知道原理,然后你自己写js fucntion 然后像上面那样封装

如何用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

js控制页面滚动(实例)

目标描述:多个图片排列下来,按右边的小按钮,抵达相应位置,鼠标滑动,抵达下一图,或者上一图

知识点:onmousewheel,addEventListener,scrollTo,setTimeout

过程:

1.body 宽,高钉死,100vw,100vh,overflow:hidden 使得不出现滚动条,不然不好看

2.图片放进去,排起来,(注意:默认空隙的处理,可以使用flex布局,空隙就不见了)

3.制作相对于视窗的按钮,几张图片就几个按钮,(position: fixed;计算一下高度,可以利用calc计算top使得上下居中)

4.美化一下,css写写

5.先写简单的按钮事件

6.写监听滑动事件(onmousewheel在火狐无效,DOMMouseScroll只在火狐有效)

react在componentDidMount的时候监听

7.补充写一下火狐的

9.测试检查一下。

完成啦,啦啦啦~

我的截图:

缺点:这里我是一直对页面进行监听,导致滑动过快对时候动画效果开始执行对时间延后。体现为滑动对轻,整个就流畅一点。

ps:写这种带计算带页面,我觉得是考验思维的,你可以对这里的知识点不熟练,但是你必须得能理解每一步的加加减减。

如何用JavaScript实现鼠标滚轮滑动效果

javascript:function AutoScroll() {window.scrollBy(0,10); NextScroll = setTimeout('AutoScroll()', 120);window.onkeydown = StopScroll;};function StopScroll(e) {if(e.keyCode == 27){clearTimeout(NextScroll);};};AutoScroll();好像是这个吧-。-simple U里就有不过想用它替代滚轮 显然还不现实[hr]来自于世界上最奇葩的:Opera/9.80 (Windows NT 6.2; WOW64) Presto/2.12.388 Version/12.13


当前名称:javascript滑动,js触摸滑动
路径分享:http://cdkjz.cn/article/dsdjogo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220