jquery - Background center parallax effect jumps to top -


i using below code provide simple parallax effect background image. ideally in css want image centered (not top). however, start scrolling script jumps top , begins effect. cant work out if it's possible alter jquery begins centred image? can help?

<div id="para" style="background-image: url('blah')" data-speed="8" data-type="background"> </div>  #para {     height:500px;     float:left;     width:100%;     background-size:100%;     background-position: center center;     background-repeat: no-repeat;     background-attachemnt: fixed;  }  //parallax $(document).ready(function() {     if ($(window).width() > 1025) {         // cache window object         $window = $(window);         $('div[data-type="background"]').each(function() {             var $bgobj = $(this);             $(window).scroll(function() {                 var ypos = -($window.scrolltop() / $bgobj.data('speed'));                 var coords = '50% ' + ypos + 'px';                 $bgobj.css({                     backgroundposition: coords                 });             });         });     } }); 

you can't give background-position percentage , pixels. should 1 or other. use percent units , turn $window.scrolltop() percentage well. multiply 100 , divide $window.height().

//parallax $(document).ready(function() {      if ($(window).width() > 1025) {         // cache window object         $window = $(window);         $('div[data-type="background"]').each(function() {             var $bgobj = $(this);             $(window).scroll(function() {                 var ypos = 50 - ($window.scrolltop() * 100 / $window.height() / $bgobj.data('speed'));                 var coords = '50% ' + ypos + "%";                 $bgobj.css({                     backgroundposition: coords                 });             });         });     } }); 

your data-speed attribute needs lower too. found 0.5 worked well.

<div id="para" style="background-image: url('blah')" data-speed="0.5" data-type="background"> 


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -