如何在jQuery中创建左右按钮来移动产品轮播?

调查并一点一点地整理我的代码,我已经实现了一个带有mouseup功能的轮播,它允许我通过按下鼠标左键而不释放它来移动产品,到目前为止它运行得很好,有时它仍然存在由于停顿,也就是说,如果我移动指针移动产品,则无需按下。

我想在我的代码中实现的是能够集成两个按钮,一个右一个左,也能够以这种方式移动轮播的产品。我怎样才能实现它,你能给我解释一下吗?

    var direction_slider = "up";
    var current_step = 0;
    var scroll_product = false;
    var scroll = -1;

$(function(){
    // vars for clients list carousel 
    var $product_carousel = $('.slider');
    var products = $product_carousel.children().length;
    var product_width = (products * 140); // 140px width for each client item 
    $product_carousel.css('width',product_width);

    var rotating = true;
    //var product_speed = 1800;
    //var see_products = setInterval(rotateClients, product_speed);

    $(document).on({
        mouseenter: function(){
            rotating = false; // turn off rotation when hovering
        },
        mouseleave: function(){
            rotating = true;
        }
    }, '#carousel');

    $product_carousel.on("mousedown", function(e) {
        scroll_product = true;
        scroll = e.pageX;
        event.preventDefault();
    }).on("mouseup", function(e) {
        scroll_product = false;
        var num = Math.floor(Math.abs(scroll - e.pageX) / 140);
        var dir = scroll - e.pageX < 0 ? "up" : "down";
        for (var x = 0; x < num; x++) {
            var $first = $('.slider .item:first');
            var $last  = $('.slider .item:last');
            if (dir == "up") {
                $last.prependTo(".slider");
            } else {
                $first.appendTo(".slider");
            }
        }
        $(".slider").css("transform", "translate(0, 0)")
    }).on("mousemove", function(e) {
        if (scroll_product) {
            $(".slider").css("transform", "translate(" + ( e.pageX - scroll ) +"px, 0)")
        }
    });
});
.carousel {
    margin: 0 auto;
    padding: 1em;
    width: 100%;
    max-width: 1170px;
    margin-left: auto;
    overflow: hidden;
}
.slider {
    width: 100% !important;
    display: flex;
}
.item {
    display: inline-table;
    width: 280px;
    height: 325px;
    margin: 0.5em;
    border: 1px solid #ccc;
}
a {
    color: #8563CF;
    text-decoration: none;
    font-weight: 100;
}
.thumbnail {
    height: 150px;
    position: relative;
}
.thumbnail img {
    height: 100%;
    width: 100%;
    object-fit: cover;
    object-position: 50% 15%;
}

img {
    border: 0;
    height: auto;
    max-width: 100%;
    vertical-align: middle;
}
.p1em {
    padding: 1em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div>
                                        <div>
                                            <div>
                                                    <div>
        <a href="#">
            <div>
                <img src="https://i.ytimg.com/vi/ZxrUVuOqsy0/maxresdefault.jpg">
            </div>
            <div>
                <div>
                    <h3>Prueba 1</h3>
                </div>
                <div>
                    <span></span>
                </div>
                                <div>
                    <p>
                        <label></label>
                        <em>$40.130,00</em>
                    </p>
                </div>
                            </div>
        </a>
    </div>  <div>
        <a href="#">
            <div>
                <img src="https://i.ytimg.com/vi/ZxrUVuOqsy0/maxresdefault.jpg">
            </div>
            <div>
                <div>
                    <h3>Curso de PHP 8 básico, intermedio y, avanzado. </h3>
                </div>
                <div>
                    <span>Acaded</span>
                </div>
                                <div>
                    <button>Ir al curso</button>
                </div>
                            </div>
        </a>
    </div>

                                            </div>
                                        </div>
                                    </div>
以上是如何在jQuery中创建左右按钮来移动产品轮播?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>