简体   繁体   English

jQuery动画到视口顶部

[英]jQuery animate to top of the viewport

I'm creating a product page for an e-commerce website which has a fixed bar at the top of the page and the basket within that. 我正在为电子商务网站创建一个产品页面,该网站的页面顶部有一个固定的栏,其中有一个篮子。 There are lots of items on the page and hence there are lots of 'Add To Basket' buttons. 页面上有很多项目,因此有很多“添加到购物篮”按钮。

I need the Add To Basket button to fly up to the basket at the fixed bar. 我需要添加到篮子按钮飞到固定栏上的篮子。 At the moment, I've managed to get the box to appear where I need it to appear when clicked, however there isn't any transition with the animation. 目前,我已经设法让盒子显示在我点击时需要它出现的位置,但是动画没有任何转换。 I'm guessing it could be the fact that I'm turning the object into a position:fixed; 我猜这可能是因为我正在将物体转变为一个position:fixed; .

Here's a fiddle of what I've got so far.. Hope you can help! 这是我到目前为止的一个小提琴..希望你能帮忙!

http://jsfiddle.net/d7tHU/ http://jsfiddle.net/d7tHU/

$('.addToCart').click(function(){
    $(this).css('position','fixed');
    $(this).animate({
        top: '0px',
        right:'0px'
    }, "slow");

});

一个使用transition属性的CSS解决方案,对JavaScript的依赖性较小: http//jsfiddle.net/d7tHU/10/

I guess what you want is this ' http://jsfiddle.net/d7tHU/11/ '. 我想你想要的是' http://jsfiddle.net/d7tHU/11/ '。

$('.addToCart').click(function(){
    $(this).css('position','absolute');
    $(this).animate({
        top: -$(this).offset().top+'px',
        left: ($('#basket').offset().left - $('#basket').width()) +'px'
    }, "slow", function(){
        $(this).hide().appendTo('#basket')
           .css({position:'static', float:'right'})
           .fadeIn()
           .off('click');
        $('.price').html(parseFloat($('.price').html())+12.5);
    });    
});

The HTML little mod: HTML小mod:

<div id='header'>
    <div id='basket'>
        <span style="padding-right:10px">Price: <span class="price">0</span>€</span>
    </div>
</div>

I think this might send you in the right direction. 我想这可能会让你朝着正确的方向前进。

http://jsfiddle.net/y7W4N/12/ http://jsfiddle.net/y7W4N/12/

You are targeting the inner div, target the parent as that is what you want to move ie $( ".addToCart").parent() , 你的目标是内部div,目标是父你想要移动的东西,即$( ".addToCart").parent()

Also your script is changing position, ie left, but you have margin-left... so just work on top left etc. 你的脚本也在改变位置,即左边,但你有左边距...所以只需要在左上角等工作。

EDIT a better version http://jsfiddle.net/b5Uux/ <-wrong link this is better 编辑更好的版本http://jsfiddle.net/b5Uux/ <-wrong链接这是更好的

http://jsfiddle.net/b5Uux/1/ http://jsfiddle.net/b5Uux/1/

I have a working version for you here http://jsfiddle.net/ZET98/ 我在这里有一个工作版本http://jsfiddle.net/ZET98/

What I did was changed the positions from margin-top and margin-left to top and right 我所做的是改变了从margin-topmargin-lefttopright

I also changed your javascript to animate the when the parent is clicked and update the top css value 我还更改了您的javascript以动画单击父级时的动画并更新top css值

$( ".addToCart").parent()
.click(function() {
  $(this)
  .css({
      position:'fixed',
      top: $(this).offset().top - $(window).scrollTop()+'px'
  })
  .animate({
    right: $(this).children('.addToCart').outerWidth()+'px',
    top:"0",
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM