Background:
I am trying to create a system that allows me to scroll the map view using the mouse, scroll up and scroll the mouse like this:
------- | | | 1 | | | ------- ------- ------- ------- ------- | | | | | | | | | 2 | | 3 | | 4 | | 5 | | | | | | | | | ------- ------- ------- ------- ------- | | | 6 | | | -------
Each of the above reset fields in javascript is the height and width of the browser. When you scroll up and down, the browser will scroll to an equal position on the map, starting with div 1, then 2, and so on. This positioning is set using my scroll function, which listens for mouse scrolling and adds the appropriate padding to the top and edge to the left to create the illusion of the map above.
Here is the code for it:
http://codepen.io/lorenzoi/pen/mxejJ
And here is the javascript for it:
$(function(){ $(window).on('resize', function(){ var $divs = $('.vertical, .horizontal > div'), ww = $(this).width(), wh = $(this).height(); $divs.css({ height : wh, width : ww }); $('.horizontal').each(function(){ var $container = $(this), nbChildren = $container.children().length; posY = $container.offset().top, $container.css({ height: ww*(nbChildren-1) + wh, width: ww*(nbChildren) }); }); }).trigger('resize'); $(window).on('scroll', function(){ var wh = $(window).height(), scroolTop = $(window).scrollTop(); $('.horizontal').each(function(){ var $container = $(this), posY = $container.offset().top, height = $container.height(), nbChildren = $container.children().length, marginMax = $container.width()/nbChildren*(nbChildren-1), marginL = scroolTop - posY; if(marginL<0) marginL = 0; else if (marginL>marginMax) marginL = marginMax; $container.css('marginLeft', -marginL); $container.css('paddingTop', marginL); }); }).trigger('scroll'); });
Problem:
Resizing the window creates a strange div offset, which is fixed when the scroll function is called for sections 2 through 5. When resizing, when in these divs, they seem to compensate themselves, and then only lock when the scroll function is called.
.trigger ('size'); should always be called, huh? I do not know why this is only called when the scroll function is executed.
How can this be fixed?