problem with animate an scroll

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:

  1. jQuery(document).ready(function(){
  2.    
  3. var padding= 100; //frame top and bottom
  4. var divmenu_normal = 140; //menu when is ont expanded
  5. var frame_top = padding + divmenu_normal;
  6. var frame_bottom;
  7. var divmenu_read;
  8. var additional_height=0;
  9. var doc_height = jQuery('body').height();
  10. var f1_pos;
  11. var f1_pos_bottom = 0;
  12. var pos_dif = 0;
  13. var act_pos=0;
  14. var lastpos=0;

  15.     divmenu_read = jQuery("#block-menu-menu-hauptnavigation").height(); //read expanded menu
  16.     frame_bottom = doc_height-padding-jQuery('#f1').height(); // Get the bottom frame position
  17.        
  18.     var divtotal = divmenu_read + padding;
  19.    
  20.     if(divmenu_read>=divmenu_normal){
  21.         additional_height = divmenu_read-divmenu_normal - padding;
  22.     }
  23.    
  24.     f1_pos = jQuery('#f1').position().top+additional_height; 
  25.     jQuery('#f1').css("top",f1_pos+"px"); //set the image to the right position with space to the menu
  26.    
  27.    
  28.     jQuery(window).scroll(function(){
  29.         var newpos = 0;
  30.         act_pos=jQuery(window).scrollTop();
  31.         f1_pos_bottom = jQuery('#f1').position().top;
  32.         if(act_pos>=lastpos){ //when new position is higher, its a scroll down
  33.             pos_dif = act_pos-lastpos; //the scrolled different height
  34.             lastpos=act_pos;
  35.          
  36.             if((f1_pos_bottom<=frame_bottom)&&((f1_pos_bottom+pos_dif)<=frame_bottom)){
  37.                  jQuery("#f1").animate({
  38.                     top: '+='+pos_dif,  
  39.                  }, 50, function() {
  40.                     // Animation complete.
  41.                  });
  42.             }
  43.         }else{ //its a scroll up
  44.             pos_dif = lastpos-act_pos; //the scrolled different height
  45.             lastpos=act_pos;
  46.  
  47.             if(act_pos>=frame_top){ 
  48.              jQuery("#f1").animate({
  49.                 top: '-='+pos_dif,  
  50.              }, 50, function() {
  51.                 // Animation complete.
  52.              });
  53.             }
  54.         }     
  55.     });      
  56. });

That's the container which is called(whitout menu):
  1.   <div id="f1">
  2.       <div id="float_logo_wrapper">
  3.             <div id="float_logo"></div>
  4.       </div>
  5.    </div>
and thats the css defintion:
  1. #f1 { position:absolute; top:493px; left:0px; width:100%; height:310px;}
  2. #float_logo_wrapper {width:972px; height:310px;margin: 0 auto;}
  3. #float_logo {
  4. width:310px;
  5. height:310px;
  6. background-image:url('.../be_main/images/float_logo_events.png');
  7. background-repeat:no-repeat;
  8. }









    • Topic Participants

    • simon