jQuery animate from CSS “top” to “bottom”

后端 未结 9 1858
北海茫月
北海茫月 2021-01-04 06:49

I\'m looking to animate a div element from an absolute top position to an absolute bottom position on the page load.

The combination of CSS and jQuery code below fai

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-04 07:32

    You can set top:auto with .css method and then animate:

    $(document).ready(function(){
       $("#line-three").css({top:'auto'});   
       $("#line-three").animate({bottom:'100px'}, 200)   
    })
    

    EDIT:

    You can play with size of body/screen and convert top position to bottom position and then animate to the desired bottom position:

    $(document).ready(function(){
      var bodyHeight = $('body').height();
      var footerOffsetTop = $('#line-three').offset().top;
      var topToBottom = bodyHeight -footerOffsetTop;
    
      $('#line-three').css({top:'auto',bottom:topToBottom});
      $("#line-three").delay(100).animate({
        bottom: '100px',
      }, 1200); 
    

    })

    Example: http://jsfiddle.net/reWwx/4/

提交回复
热议问题