简体   繁体   English

移动 div 块

[英]Move div blocks with

I need make something like slider.我需要制作类似滑块的东西。 But I have problem with jQuery I don't know how right force to move my items.但是我对 jQuery 有问题,我不知道移动我的物品的力道如何。 Maybe I make not properly css file and maybe this need change?也许我制作的 css 文件不正确,也许这需要更改? Help please.请帮忙。

I have html:我有 html:

<div class="carusel-section">
    <div class="top-indent">
        <div class="carusel-container">
            <div class="arrow-back">
                <img src="img/arrow-back.png" alt="back">
            </div>
            <div class="item item-first"><h4 class="item-text">200x120</h4></div>
            <div class="item"><h4 class="item-text">200x120</h4></div>
            <div class="item"><h4 class="item-text">200x120</h4></div>
            <div class="item"><h4 class="item-text">200x120</h4></div>
            <div class="item"><h4 class="item-text">200x120</h4></div>
            <div class="item"><h4 class="item-text">200x120</h4></div>
            <div class="arrow-forward">
                <img src="img/arrow-forward.png" alt="forward">
            </div>
        </div>
    </div>
</div>

Css: css:

.carusel-container {
    max-width: 742px;
    margin-left: 5px;
}

    .carusel-container:before {
        width: 40px;
        left: 17px;
    }

    .carusel-container .arrow-back {
        top: 43px;
        left: 28px;
    }

    .carusel-container .item-first {
        margin: 0 0 0 57px;
    }

    .carusel-container .arrow-forward {
        top: 43px;
        right: 12px;
    }

jQuery: jQuery:

$(".arrow-back").click(function () {
    console.log("BACKKK");
    $(".item").css("margin-left", "-50px");
});

$(".arrow-forward").click(function () {
    console.log("TOWARDDD");
    $(".item").css("margin-right", "-50px");
});

Initially, one element should appear only and all the others should be hidden最初,一个元素应该只出现,其他所有元素都应该隐藏

On pressing the next button, all the elements should disappear except the next element, and same thing to back button按下下一个按钮时,除下一个元素外,所有元素都应该消失,与后退按钮相同

I recommend you to use this simple code:我建议你使用这个简单的代码:

HTML Code: HTML代码:

<div id="carousel-container">
        <a href="#" class="control_next">>></a>
        <ul>
            <li>200x120</li>
            <li>200x120</li>
            <li>200x120</li>
            <li>200x120</li>
            <li>200x120</li>
            <li>200x120</li>
        </ul>
        <a href="#" class="control_prev"><<</a>
</div>

CSS Code: CSS 代码:

#carousel-container {
    position: relative;
    overflow: hidden;
    margin: 20px auto 0 auto;
    border-radius: 4px;
}
#carousel-container ul {
    position: relative;
    margin: 0;
    padding: 0;
    height: 200px;
    list-style: none;
}
#carousel-container ul li {
    position: relative;
    display: block;
    float: left;
    margin: 0;
    padding: 0;
    width: 500px;
    height: 300px;
    background: #ccc;
    text-align: center;
    line-height: 300px;
}
a.control_prev,
a.control_next {
    position: absolute;
    top: 40%;
    z-index: 999;
    display: block;
    padding: 4% 3%;
    width: auto;
    height: auto;
    background: #2a2a2a;
    color: #fff;
    text-decoration: none;
    font-weight: 600;
    font-size: 18px;
    opacity: 0.8;
    cursor: pointer;
}
a.control_prev:hover,
a.control_next:hover {
    opacity: 1;
    -webkit-transition: all 0.2s ease;
}
a.control_prev {
    border-radius: 0 2px 2px 0;
}
a.control_next {
    right: 0;
    border-radius: 2px 0 0 2px;
}

JS Code: JS代码:

jQuery(document).ready(function ($) {

    var slideCount = $('#carousel-container ul li').length;
    var slideWidth = $('#carousel-container ul li').width();
    var slideHeight = $('#carousel-container ul li').height();
    var sliderUlWidth = slideCount * slideWidth;

    $('#carousel-container').css({ width: slideWidth, height: slideHeight });

    $('#carousel-container ul').css({ width: sliderUlWidth, marginLeft: - slideWidth });

    $('#carousel-container ul li:last-child').prependTo('#carousel-container ul');

    function moveLeft() {
        $('#carousel-container ul').animate({
            left: + slideWidth
        }, 200, function () {
            $('#carousel-container ul li:last-child').prependTo('#carousel-container ul');
            $('#carousel-container ul').css('left', '');
        });
    };

    function moveRight() {
        $('#carousel-container ul').animate({
            left: - slideWidth
        }, 200, function () {
            $('#carousel-container ul li:first-child').appendTo('#carousel-container ul');
            $('#carousel-container ul').css('left', '');
        });
    };

    $('a.control_prev').click(function () {
        moveLeft();
    });

    $('a.control_next').click(function () {
        moveRight();
    });

});  

Check it out.一探究竟。

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

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