problem with animate an scroll
hi @ll
i wanna place a picture on a fixed position under a vertical menu and the menu can expand. and in the bottom i have a frame. when page load, the position should be calculated with the right margin and when the user scroll the image have to stop bevor it reach the frame of the bottom. and that is the problem. when i scrol slowely, it works, but when i scroll fast, the picture moves over the frame and that shoudn't be. i think it's cause when the scroll event is called again and its still in an animation, the maximum limit is not reached at this moment, so it will do the animation with wrong values again and again. i have tried it in so much ways but nothing worked. can someone hellp me? have someone an idea how can i solve hat?
here is the code:
- jQuery(document).ready(function(){
-
- var padding= 100; //frame top and bottom
- var divmenu_normal = 140; //menu when is ont expanded
- var frame_top = padding + divmenu_normal;
- var frame_bottom;
- var divmenu_read;
- var additional_height=0;
- var doc_height = jQuery('body').height();
- var f1_pos;
- var f1_pos_bottom = 0;
- var pos_dif = 0;
- var act_pos=0;
- var lastpos=0;
- divmenu_read = jQuery("#block-menu-menu-hauptnavigation").height(); //read expanded menu
- frame_bottom = doc_height-padding-jQuery('#f1').height(); // Get the bottom frame position
-
- var divtotal = divmenu_read + padding;
-
- if(divmenu_read>=divmenu_normal){
- additional_height = divmenu_read-divmenu_normal - padding;
- }
-
- f1_pos = jQuery('#f1').position().top+additional_height;
- jQuery('#f1').css("top",f1_pos+"px"); //set the image to the right position with space to the menu
-
-
- jQuery(window).scroll(function(){
- var newpos = 0;
- act_pos=jQuery(window).scrollTop();
- f1_pos_bottom = jQuery('#f1').position().top;
- if(act_pos>=lastpos){ //when new position is higher, its a scroll down
- pos_dif = act_pos-lastpos; //the scrolled different height
- lastpos=act_pos;
-
- if((f1_pos_bottom<=frame_bottom)&&((f1_pos_bottom+pos_dif)<=frame_bottom)){
- jQuery("#f1").animate({
- top: '+='+pos_dif,
- }, 50, function() {
- // Animation complete.
- });
- }
- }else{ //its a scroll up
- pos_dif = lastpos-act_pos; //the scrolled different height
- lastpos=act_pos;
-
- if(act_pos>=frame_top){
- jQuery("#f1").animate({
- top: '-='+pos_dif,
- }, 50, function() {
- // Animation complete.
- });
- }
- }
- });
- });
That's the container which is called(whitout menu):
- <div id="f1">
- <div id="float_logo_wrapper">
- <div id="float_logo"></div>
- </div>
- </div>
and thats the css defintion:
- #f1 { position:absolute; top:493px; left:0px; width:100%; height:310px;}
- #float_logo_wrapper {width:972px; height:310px;margin: 0 auto;}
- #float_logo {
- width:310px;
- height:310px;
- background-image:url('.../be_main/images/float_logo_events.png');
- background-repeat:no-repeat;
- }