【问题标题】:How to make fly to cart animation without jQuery? [closed]如何在没有 jQuery 的情况下制作飞到购物车的动画? [关闭]
【发布时间】:2021-12-10 10:48:36
【问题描述】:

我想创建“飞到购物车动画”,但我不知道怎么做。我找到了Jquery,但我不需要它。我只想使用纯JS

【问题讨论】:

  • 希望您至少尝试自己编写代码。我建议你做一些additional research,通过谷歌或搜索SO,尝试一下。如果您仍然遇到问题,请返回您的代码并解释您尝试过的操作。
  • 我建议使用 CSS 动画或过渡效果,并在效果之后调用 JS ontransitionend 事件将商品实际添加到购物车。另一种方法是研究 jQuery 插件并将其重写为 javascript。如果您不知道如何重写某些特定部分,请在代码中发布详细信息以获取帮助。您所说的问题不够集中,无法回答。

标签: javascript html jquery css reactjs


【解决方案1】:

/*
    Add to cart fly effect with jQuery. - May 05, 2013
    (c) 2013 @ElmahdiMahmoud - fikra-masri.by
    license: https://www.opensource.org/licenses/mit-license.php
*/   

$('.add-to-cart').on('click', function () {
        var cart = $('.shopping-cart');
        var imgtodrag = $(this).parent('.item').find("img").eq(0);
        if (imgtodrag) {
            var imgclone = imgtodrag.clone()
                .offset({
                top: imgtodrag.offset().top,
                left: imgtodrag.offset().left
            })
                .css({
                'opacity': '0.5',
                    'position': 'absolute',
                    'height': '150px',
                    'width': '150px',
                    'z-index': '100'
            })
                .appendTo($('body'))
                .animate({
                'top': cart.offset().top + 10,
                    'left': cart.offset().left + 10,
                    'width': 75,
                    'height': 75
            }, 1000, 'easeInOutExpo');
            
            setTimeout(function () {
                cart.effect("shake", {
                    times: 2
                }, 200);
            }, 1500);

            imgclone.animate({
                'width': 0,
                    'height': 0
            }, function () {
                $(this).detach()
            });
        }
    });
.wrapper {
    width: 705px;
    margin: 20px auto;
    padding: 20px;
}
h1 {
    display: inline-block;
    background-color: #333;
    color: #fff;
    font-size: 20px;
    font-weight: normal;
    text-transform: uppercase;
    padding: 4px 20px;
    float: left;
}
.clear {
    clear: both;
}
.items {
    display: block;
    margin: 20px 0;
}
.item {
    background-color: #fff;
    float: left;
    margin: 0 10px 10px 0;
    width: 205px;
    padding: 10px;
    height: 290px;
}
.item img {
    display: block;
    margin: auto;
}
h2 {
    font-size: 16px;
    display: block;
    border-bottom: 1px solid #ccc;
    margin: 0 0 10px 0;
    padding: 0 0 5px 0;
}
button {
    border: 1px solid #722A1B;
    padding: 4px 14px;
    background-color: #fff;
    color: #722A1B;
    text-transform: uppercase;
    float: right;
    margin: 5px 0;
    font-weight: bold;
    cursor: pointer;
}
span {
    float: right;
}
.shopping-cart {
    display: inline-block;
    background: url('http://cdn1.iconfinder.com/data/icons/jigsoar-icons/24/_cart.png') no-repeat 0 0;
    width: 24px;
    height: 24px;
    margin: 0 10px 0 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<!-- wrapper -->
<div class="wrapper">
    <h1>Bike Stock</h1>
    <span><i class="shopping-cart"></i></span>

    <div class="clear"></div>
    <!-- items -->
    <div class="items">
        <!-- single item -->
        <div class="item">
            <img src="https://via.placeholder.com/250/164/FFFFFF/.png?text=Item%20[1]" alt="item"/>
            <h2>Item 1</h2>

            <p>Price: <em>$449</em>
            </p>
            <button class="add-to-cart" type="button">Add to cart</button>
        </div>
        <!--/ single item -->
        <!-- single item -->
        <div class="item">
            <img src="https://via.placeholder.com/250/c00/FFFFFF/.png?text=Item%20[2]" alt="item"/>
            <h2>Item 1</h2>

            <p>Price: <em>$449</em>
            </p>
            <button class="add-to-cart" type="button">Add to cart</button>
        </div>
        <!--/ single item -->
        <!-- single item -->
        <div class="item">
            <img src="https://via.placeholder.com/250/c60/FFFFFF/.png?text=Item%20[3]" alt="item"/>
            <h2>Item 1</h2>

            <p>Price: <em>$449</em>
            </p>
            <button class="add-to-cart" type="button">Add to cart</button>
        </div>
        <!--/ single item -->
    </div>
    <!--/ items -->
</div>
<!--/ wrapper -->

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 2015-05-02
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    相关资源
    最近更新 更多