如何使用CSS创建副本和图像,移动和缩放

时间:2016-06-21 03:13:42

标签: css css-transitions

我有一个AngularJS应用程序,我有一个"添加到购物车"按钮。我想要的是,当有购物车图标时,图像被复制并移动(并缩小)到页面顶部。有谁知道我会在哪里找到解决方案?

我目前的CSS一半有效。它将图像移动到图像的div的左上角,使图像变小。我想要做的是将图像带到位于页面顶部的购物车div。

.slideOutUp:hover {
  -webkit-animation-name: slideOutUp;
  animation-name: slideOutUp;
  -webkit-animation-duration: .5s;
  animation-duration: .5s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  }
  @-webkit-keyframes slideOutUp {
  0% {
  -webkit-transform: translateY(0);
  transform: translateY(0);
  }
  100% {
  visibility: hidden;
  -webkit-transform: scale3d(.3, .3, .3);
  transform: scale3d(.3, .3, .3);
  transform-origin: left top;   
  }
  }
  @keyframes slideOutUp {
  0% {
  -webkit-transform: translateY(0);
  transform: translateY(0);
  }
  100% {
  visibility: hidden;
  -webkit-transform: scale3d(.3, .3, .3);
  transform: scale3d(.3, .3, .3);
  transform-origin: left top;
  }
  } 

1 个答案:

答案 0 :(得分:0)

不完美,但在这里..

http://codepen.io/poldiwa/pen/yJadjN



$cart = $('.cart');
/* get details about the cart div */
var t = $cart.offset().top;
var l = $cart.offset().left;
var w = parseInt($cart.css('width').replace('px',''));
var h = parseInt($cart.css('height').replace('px',''));
var vCenter = (t+h)/2; // vertical center of cart div
var hCenter = (l+w)/2; // horizontal center of cart div
$('a').on('click',function(e){
  e.preventDefault();
  $orig = $(this).siblings('img');
  $clone = $orig.clone();
  //make our duplicate for animation
  /* put our clone in the same position */
  $clone.css('position','fixed');
  $clone.css('top',$orig.offset().top);
  $clone.css('left',$orig.offset().left);
  
  $('body').append($clone);
  // add to the dom so we can see it.
  // then animate
  $.when($clone.animate({
    top: vCenter,
    left: hCenter,
    width: '25px',
    opacity: 0.2
  })).done(function(){
    // when the animation is done,
    // we remove it from the DOM
    $(this).remove();
  });
});

*{
  box-sizing: border-box;
}
html,body{
  color: #333;
}
.cart{
  border: 1px solid #ddd;
  margin: 1em 0;
  padding: 1em;
  width: 10%;
  text-align: center;
}
.row{
  display: flex;
  justify-content: space-around;
}
.col{
  position: relative;
}
.col a{
  position: absolute;
  text-align: center;
  bottom: 0;
  background: #ddd;
  padding: 1em;
  width: 100%;
  color: inherit;
  text-decoration: none;
  /* z-index to prevent the clone covering the button */
  z-index: 1;
}

<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="cart"><i class="fa fa-shopping-cart"></i></div>
<div class="row">
  <div class="col">
    <img src="http://placehold.it/250/000000/ffffff" alt="" />
    <a href="#"><i class="fa fa-cart-plus"></i> Add to Cart</a>
  </div>
  <div class="col">
    <img src="http://placehold.it/250/000000/ffffff" alt="" />
    <a href="#"><i class="fa fa-cart-plus"></i> Add to Cart</a>
  </div>
  <div class="col">
    <img src="http://placehold.it/250/000000/ffffff" alt="" />
    <a href="#"><i class="fa fa-cart-plus"></i> Add to Cart</a>
  </div>
</div>
&#13;
&#13;
&#13;