Using native JS package webapp sliding effect (inertial sliding, sliding Rebound)
Last Update:2017-06-14
Source: Internet
Author: User
<span id="Label3"></p><p><p>PC Mobile compatible with IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+ inertia booster, sliding rebound</p></p><p><p>Façade mode</p></p>Window.onload = function () {<br>/* test Data */<br>var insert = ';<br>for (var i = 0; I <; i++) {<br>Insert + = ' <div style = ' width:100%; text-align:center; " > Slide Test ' + i + ' </div> ';<br>}<br>document.getElementById ("movearea"). InnerHTML = insert;<br>/* test Data */<br>var at = new Apptouch ({<br>Tcontain: ' Apparea ',//required: sliding area ID<br>Tmove: ' Movearea ',//required: move area ID<br>Tscroller: ' scroller ',//required: custom scroll bar<br>Tscrollerarea: ' Scrollerarea '//required: scroll bar area<br>}, onmoveend);<br>Top/bottom Callback<br>function Onmoveend (M) {<br>Console.log (m);<br>}<p><p></p></p><p><p>}<br>/*=====================<br>* Name: Apptouch<br>* Function: Web App Slide simulation component<br>* Parameters: Related configuration<br>======================*/<br>var Apptouch = function (config, Callback) {<br>This.touchcontain = config.tcontain;<br>This.touchmove = config.tmove;<br>This.touchscroller = config.tscroller;<br>This.touchscrollerarea = config.tscrollerarea;<br>THIS.CALLBACKFN = callback;<br>This.move ();<br>}</p></p><p>Apptouch.prototype = {<br>Move:function (e) {<br>var monitor = document.getElementById (this.touchcontain),//monitoring container<br>target = document.getElementById (this.touchmove),//moving target<br>Scroller = document.getElementById (this.touchscroller),//custom scroll bar<br>Scrollerarea = document.getElementById (this.touchscrollerarea),//scroll bar area<br>Sheight = monitor.offsetheight/target.offsetheight * Monitor.offsetheight,//custom scroll bar length<br>st = (target.offsetheight-monitor.offsetheight)/(monitor.offsetheight-sheight),//moving blocks corresponding to the wheel unit length<br>Tslow = 4,//top/bottom minus base<br>Tmove = 0,//slider to top top value<br>Tmovel = Tmove + 140,//top allow Drop-down range<br>Bmove = monitor.offsetheight-target.offsetheight,//thumb Top Value<br>Bmovel = bMove-140,//to allow the upper sliding range<br>CALLBACKFN = This.callbackfn,//callback function<br>FLG = False,//mark whether swipe<br>Starty,//mark Start position<br>Starttop,//mark the height value of the slide start<br>move = 0;<br>Moving distance<br>Mouse Event Registration<br>Addevent (monitor, ' MouseDown ', movestart);<br>Addevent (monitor, ' MouseMove ', movein);<br>Addevent (monitor, ' MouseUp ', moveEnd);<br>Addevent (window, ' MouseMove ', movein);<br>Addevent (window, ' MouseUp ', moveEnd);<br>Mobile Device Touch Event Registration<br>Addevent (monitor, ' Touchstart ', movestart);<br>Addevent (monitor, ' Touchmove ', movein);<br>Addevent (monitor, ' Touchend ', moveEnd);<br>/**<br>* Exterior/façade Mode Packaging<br>*/<br>/* Event Monitoring */<br>function Addevent (el, type, Fn) {<br>If (el.addeventlistener) {<br>El.addeventlistener (type, fn, false);<br>} else if (el.attachevent) {<br>El.attachevent (' on ' + type, fn);<br>} else {<br>el[' on ' + type] = fn;<br>}<br>}</p><p><p>Cancel Browser default behavior<br>function Stop (e) {<br>Opera/chrome/ff<br>If (e.preventdefault)<br>E.preventdefault ();<br>Ie<br>E.returnvalue = false;<br>}</p></p><p><p>End of package<br>/**<br>* Operation function<br>*/<br>Inertial easing parameters<br>var lastmovetime = 0;<br>var Lastmovestart = 0;<br>var stopinertiamove = false;<br>/* Move Trigger */<br>function MoveStart (e) {<br>Stop (e);<br>FLG = true;<br>If (e.touches)<br>E = e.touches[0];<br>Starty = e.clienty;<br>Starttop = Target.style.top | | 0;<br>Inertia Slow motion<br>Lastmovestart = starty;<br>Lastmovetime = new Date (). getTime ();<br>Stopinertiamove = true;<br>scrollerArea.style.visibility = ' Visible ';</p></p><p><p>}</p></p><p><p> /* during move */<br> function movein (e) {<br> if (flg) {<br> stop (e); <br> if (e.touches) <br> e = e.touches[0]; <br> move = E.clienty-starty + parseint (starttop); <br> if (move > Tmove) {<br> (move-tmove)/tslow + tmove > TM ovel? move = Tmovel:move = (move-tmove)/tslow + tmove </p></p><p><p>} else if (move < Bmove)<br>(move-bmove)/tslow + bmove < bmovel? move = Bmovel:move = (move-bmove)/tslow + bmove;<br>Target.style.top = move + ' px ';<br>Scroller.style.top =-move/st + ' px ';<br>Inertia Slow motion<br>var nowtime = new Date (). getTime ();<br>Stopinertiamove = true;<br>If (nowtime-lastmovetime > 300) {<br>Lastmovetime = nowtime;<br>Lastmovestart = e.clienty;<br>}<br>}<br>}</p></p><p>/* Move End */<br>function MoveEnd (e) {<br>Stop (e);<br>If (e.touches)<br>E = e.touches[0];<br>Inertia Slow motion<br>var contenttop = target.style.top.replace (' px ', ');<br>var contenty = (parseint (contenttop) + e.clienty-lastmovestart);<br>var nowtime = new Date (). getTime ();<br>var v = (e.clienty-lastmovestart)/(nowtime-lastmovetime);<br>Last period of time finger paddling speed<br>Stopinertiamove = false;<br>(function (v, startTime, Contenty) {<br>var dir = v > 0? -1:1;<br>Acceleration direction<br>var deceleration = dir * 0.005;<br>function Inertiamove () {<br>If (stopinertiamove)<br>Return<br>var nowtime = new Date (). getTime ();<br>var t = nowtime-starttime;<br>var nowv = v + t * deceleration;<br>var Movey = (v + NOWV)/2 * t;<br>The speed direction changes to 0.<br>If (dir * NOWV > 0) {<br>If (move > Tmove) {<br>CALLBACKFN (' Top Up ');<br>Target.style.top = Tmove + ' px ';<br>Scroller.style.top = Tmove + ' px ';<br>} else if (move < Bmove) {<br>CALLBACKFN (' in the end ');<br>Target.style.top = bmove + ' px ';<br>Scroller.style.top =-bmove/st + ' px ';<br>}<br>SetTimeout (function () {<br>If (!stopinertiamove)<br>scrollerArea.style.visibility = ' Hidden ';<br>}, 4000);<br>Return<br>}<br>move = Contenty + movey;<br>If (move > Tmove) {<br>T/= 20;<br>move = (move-tmove)/ten + tmove;<br>} else if (move < Bmove) {<br>T/= 20;<br>move = (move-bmove)/ten + bmove;<br>}<br>Target.style.top = move + "px";<br>Scroller.style.top =-move/st + ' px ';<br>SetTimeout (inertiamove, 10);<br>}</p><p><p>Inertiamove ();<br>}) (v, nowtime, contenty);<br>move = 0;<br>FLG = false;<br>}</p></p><p><p>End of Operation<br>/**<br>* Related initialization<br>*/<br>Scroll bar Length Initialization<br>Scroller.style.height = sheight + ' px ';<br>End of initialization</p></p><p><p>},<br>Otherinteract:function () {<br>Additional Feature Extensions<br>}<br>}</p></p><p><p></p></p><p><p>IE hack CSS</p></p>body,html {background-color: #333; margin:0; height:100%; line-height:2.0; font-family: ' Microsoft yahei '; overflow-y:h idden;}<br>#contain {margin:0 auto; position:relative; width:100%; max-width:480px; _width:480px; height:100%; cursor:pointer!i mportant;}<br>#appArea {position:absolute; width:100%; height:100%; overflow:hidden; Background-color: #fff;}<br>#topInfo {position:absolute;top:60px;width:100%; height:60px; text-align:center; font-size:18px;}<br>#bottomInfo {position:absolute;bottom:0;width:100%;}<br>#scrollerArea {position:absolute; right:0; width:1.5%; height:100%;visibility:hidden;}<br>#scroller {position:absolute; top:0; width:100%; Background-color: #aaa;}<br>#moveArea {position:absolute; top:0px; width:100%; background-color: #ddd;}<p><p></p></p><p><p>HTML code</p></p><! DOCTYPE html><br>