javascript - iScroll move header up or down while scrolling -
i trying build function moves header 45px while scrolling down , reverse. should facebook header mobile app, or address-bar of chrome mobile browser or safari mobile browser. using scrolling iscroll plugin , managed let header moving while scrolling down , moving down again while scrolling up. not working. stuck while scrolling.
this js code:
function updateposition () { var wrapperposition = this.y; var laststopposition = $('#header').attr('data-last-value'); var distance = wrapperposition - laststopposition; var getmatrixstyle = window.getcomputedstyle($('#header').get(0)); var getmatrix = new webkitcssmatrix(getmatrixstyle.webkittransform); var matrixpositionheader = getmatrix.m42; var moveup = matrixpositionheader - 1; var movedown = matrixpositionheader + 1; if(wrapperposition >= -45 && wrapperposition <= 0 && distance < 0) { $('#header').css({ '-webkit-transform' : 'translate(0,' + wrapperposition + 'px)' }); } else if(wrapperposition <= -46 && distance < 0) { $('#header').css({ '-webkit-transform' : 'translate(0,-45px)' }); } else if(wrapperposition <= -46 && distance > 0) { if(matrixpositionheader <= -1){ $('#header').css({ '-webkit-transform' : 'translate(0,' + movedown + 'px)' }); } else if(matrixpositionheader == 0){ $('#header').removeattr('style'); } } else if(distance > 0 && distance <= 45){ if(matrixpositionheader <= -1){ $('#header').css({ '-webkit-transform' : 'translate(0,' + movedown + 'px)' }); } else if(matrixpositionheader == 0){ $('#header').removeattr('style'); } } writeposition.innerhtml = this.y>>0; writeposition3.innerhtml = distance; } function scrollstart() { var getmatrixstyle = window.getcomputedstyle($('#innerwrapper').get(0)); var getmatrix = new webkitcssmatrix(getmatrixstyle.webkittransform); var matrixpositionwrapper = getmatrix.m42; writeposition2.innerhtml = matrixpositionwrapper; } function scrollend() { var getmatrixstyle = window.getcomputedstyle($('#innerwrapper').get(0)); var getmatrix = new webkitcssmatrix(getmatrixstyle.webkittransform); var matrixpositionwrapper = getmatrix.m42; $('#header').attr('data-last-value', matrixpositionwrapper); writeposition4.innerhtml = matrixpositionwrapper; } function loaded () { var myscroll; writeposition = document.getelementbyid('position'); writeposition2 = document.getelementbyid('position2'); writeposition3 = document.getelementbyid('position3'); writeposition4 = document.getelementbyid('position4'); myscroll = new iscroll('#wrapper', { probetype: 3, mousewheel: true, speedratioy: 40 }); myscroll.on('scrollstart', scrollstart); myscroll.on('scroll', updateposition); myscroll.on('scrollend', scrollend); } document.addeventlistener('touchmove', function (e) { e.preventdefault(); }, false);
my html:
<div style="position:absolute; top: 110px;z-index:100; left: 120px;">innerwrapper y position: <strong id="position">0</strong> px</div> <div style="position:absolute; top: 130px;z-index:100; left: 120px;">start position: <strong id="position2">0</strong> px</div> <div style="position:absolute; top: 150px;z-index:100; left: 120px;">distance betweent start/stop position: <strong id="position3">0</strong> px</div> <div style="position:absolute; top: 170px;z-index:100; left: 120px;">last stop position: <strong id="position4">0</strong> px</div> <header id="header" role="banner" data-last-value="0"> <div class="header-main"></div> <!-- end header main --> <div class="header-bar"></div> <!-- end header bar --> </header> <!-- end header --> <div id="wrapper"> <div id="innerwrapper"> <ul> <li>content</li> ..... </ul> </div> <!-- end inner wrapper --> </div> <!-- end wrapper -->
here link testing: http://jsfiddle.net/bp9hy/1/
to see problem, try scroll few times , down stop scrolling in between (e.g. scroll 120px down , stop. scroll again).
thanks helping.
Comments
Post a Comment