使用jQuery在一个光滑的轮播点中的多个图像

时间:2018-02-14 04:01:06

标签: javascript jquery html slick.js

我在光滑的旋转木马上工作,我希望每个点都有多个图像。就像第一个点上的4个图像在移动到下一个点之前一样,然后在第二个点上移动到最后一个点之前的3个图像。

我可以使用下一个按钮执行此操作,但上一个按钮出现问题。好吧,我对下一个按钮不太确定,但至少它按照我想要的方式工作。

的index.html

<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" type="text/css" href="slick/slick.css" />
    <link rel="stylesheet" type="text/css" href="slick/slick-theme.css" />
    <meta charset="UTF-8">
    <title>Ellie</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body style="background-color: white;">
    <div class="wrap transparent-bg">
        <div>
            <ol class="ProgressBar">
                <li class="ProgressBar-step is-current">
                    <svg class="ProgressBar-icon">
                        <use xlink:href="#checkmark-bold" />
                    </svg> <span class="ProgressBar-stepLabel">Build It</span> </li>
                <li class="ProgressBar-step">
                    <svg class="ProgressBar-icon">
                        <use xlink:href="#checkmark-bold" />
                    </svg> <span class="ProgressBar-stepLabel">Connect It</span> </li>
                <li class="ProgressBar-step">
                    <svg class="ProgressBar-icon">
                        <use xlink:href="#checkmark-bold" />
                    </svg> <span class="ProgressBar-stepLabel">Activate</span> </li>

            </ol>
        </div>
        <div class="box">
            <div class="slider">
                <div class="single-item">
                    <div>
                        <div class="text">
                            <h1>Starters (1.0)</h1>
                            <p>Build the power cable (Needed: <span class="power-adapter">Power Adapter, Micro-USB cable</span> and Plug n' Play hub)</p>
                        </div>
                        <img src="img/0.png">

                    </div>
                    <div>
                        <div class="text">
                            <h1>Starters (1.1)</h1>
                            <p>Build the power cable (Needed: <span class="power-adapter">Power Adapter, Micro-USB cable</span>
                            </p>
                        </div>
                        <img src="img/1.png">

                    </div>
                    <div>
                        <div class="text">
                            <h1>Starters (1.2)</h1>
                            <p>Build the power cable (Needed: <span class="power-adapter">Power Adapter, Micro-USB cable</span>
                            </p>
                        </div>
                        <img src="img/2.png">

                    </div>
                    <div>
                        <div class="text">
                            <h1>Starters (1.3)</h1>
                            <p>Build the power cable (Needed: <span class="power-adapter">Power Adapter, Micro-USB cable</span>
                            </p>
                        </div>
                        <img src="img/3.png">

                    </div>
                    <div>
                        <div class="text">
                            <h1>Plug it in (2.0)</h1>
                            <p>Plug the power adapter in to the outlet closest to your router.</p>
                        </div>
                        <img src="img/4.png">

                    </div>
                    <div>
                        <div class="text">
                            <h1>Wait for the light (2.1)</h1>
                            <p>You should see two green lights after a few seconds</p>
                        </div>
                        <img src="img/5.png">

                    </div>
                    <div>
                        <div class="text">
                            <h1>Connect to the hub (2.2)</h1>
                            <p>Change your Wi-Fi on this device</p>
                        </div>
                        <img src="img/6.png">
                    </div>
                    <div>
                        <div class="text">
                            <h1>Activate (3.0)</h1>
                            <p>Let's go to the activation page <a href="#">Click Here</a>
                            </p>
                        </div>
                        <img src="img/7.png">
                    </div>
                </div>
            </div>
            <div class="nav-buttons">
                <div class="nav-size" id="prev-pos">
                    <a class="prev" id="pref-fl" href=""><img src="img/left.png"><span class="nav-text"></span>Previous</a>
                </div>
                <div class="nav-size" id="next-pos"><a class="next" id="next-fl" href=""><span class="nav-text">Next</span><img src="img/right.png"></a> </div>
            </div>
            <div class="clear"></div>
        </div>
    </div>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript" src="slick/slick.min.js"></script>
    <script type="script.js"></script>
</body>

</html>

的script.js

<script type="text/javascript">
    $(document).ready(function() {

        /*DEFINE PROGRESS BAR*/
        var $bar = $(".ProgressBar");

        /*SLICK OPTIONS*/
        $('.single-item').slick({
            arrows: true,
            prevArrow: $('.prev'),
            nextArrow: $('.next'),
            dots: false,
            infinite: false,
            waitForAnimate: false,
            speed: 500,
            swipe: true
        });

        $(".next").on("click", function() {

            /*Get the number of the current slide*/
            var currentSlide = $('.single-item').slick('slickCurrentSlide');

            /*Check if this is an intermediary step*/
            if (currentSlide == 2) {
                /*do nothing, this is an intermediary step*/
            } else {
                moveSlide('next');
            }

        });
        $(".prev").on("click", function() {

            /*Get the number of the current slide*/
            var currentSlide = $('.single-item').slick('slickCurrentSlide');

            /*Check if this is an intermediary step*/
            if (currentSlide == 1) {
                /*do nothing, this is an intermediary step*/
            } else {
                moveSlide('prev');
            }

        });

        /*HANDLE SWIPING*/
        $('.single-item').on('swipe', function(e, b) {
            /*IF SWIPING LEFT OR NEXT*/
            if (b.currentDirection == 0) {
                moveSlide('next');
            }
            /*IF SWIPING RIGHT OR PREV*/
            else if (b.currentDirection == 1) {
                moveSlide('prev');
            }
        })

        /*FUNCTION TO HANDLE ALL SLIDE MOVEMENT*/
        /*ACCEPTS ONE ARGUMENT 'dir' for direction*/
        function moveSlide(dir) {
            /*GET CURRENT SLIDE*/
            var currentSlide = $('.single-item').slick('slickCurrentSlide');
            if (dir == 'next') {
                if ($bar.children().last().hasClass("is-current") || (currentSlide >= 1 && currentSlide < 4) || (currentSlide >= 5 && currentSlide < 7)) {
                    //Do nothing
                } else {
                    if ($bar.children(".is-current").length > 0) {
                        $bar.children(".is-current").removeClass("is-current").addClass("is-complete").next().addClass("is-current");
                    } else {
                        $bar.children().first().addClass("is-current");
                    }
                }
            } else if (dir == 'prev') {
                if ($bar.children().first().hasClass("is-current") || (currentSlide >= 1 && currentSlide < 4) || (currentSlide >= 5 && currentSlide < 7)) {
                    //Do nothing
                } else {
                    if ($bar.children(".is-current").length > 0) {
                        $bar.children(".is-current").removeClass("is-current").prev().removeClass("is-complete").addClass("is-current");
                    } else {
                        $bar.children(".is-complete").last().removeClass("is-complete").addClass("is-current");
                    }
                }
            }
        }
    });
</script>

的style.css

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: 'HelveticaNeueLTPro-Lt';
}

.transparent-bg {
    position: static;
}

.wrap {
    max-width: 1060px;
    width: 96%;
    margin: auto; 
}

.clear:before {
    content: ' ';
    display: table;
}

.clear:after {
    content: ' ';
    display: table;
    clear: both;
}
.clear {
    clear: both;
}

.slider {
    margin-bottom: 20px;
}

.box {
    padding: 0px 20px 50px;
}

.single-item {
    padding: 20px 0 50px;
    border-bottom: 1px solid black; 
}

.single-item img {
    display: block;
    margin: 0 auto;
}

.prev, .next {
    text-decoration: none;
    font-family: sans-serif;
    font-size: 16px;
    font-family: sans-serif;
    color: #4C5C62; 
}

.nav-buttons img {
    width: 20px;
    height: 15px;
    display: inline-block;
    vertical-align:top;
}

.slick-dots {
  top: 0;  
}

.text {
    font-family: sans-serif;
    color: #4C5C62;
    padding-bottom: 50px;
}

.text h1 {
    font-size: 28px;
}

.text p {
    font-size: 16px;
}

.nav-text {
    padding: 0 10px;
}

.power-adapter {
    font-weight: 700;
}

.nav-size {
    /*width: 480px;*/
}
#prev-pos {   
    float: left;
    /*border-right: 1px solid black;*/
}

#next-pos {
    float: right;
}

#prev-fl {
    float: left;
}
#next-fl {
    float: right;
}

.slick-dotted.slick-slider {
    margin-bottom: 10px !important;
}

.ProgressBar {
    margin: 0 auto;
    padding: 2em 0 3em;
    list-style: none;
    position: relative;
    display: flex;
    justify-content: space-between;
}

.ProgressBar-step {
    text-align: center;
    position: relative;
    width: 100%;
    &:before,
    &:after {
    content: "";
    height: 0.5em;
    background-color: $gray;
    position: absolute;
    z-index: 1;
    width: 100%;
    left: -50%;
    top: calc(50% - .5em);
    transform: translateY(-50%);
    transition: all .25s ease-out;
}

&:first-child:before,
&:first-child:after { 
    display: none; 
}

&:after {
    background-color: $blue;
    width: 0%;
}

&.is-complete + &.is-current:after,
&.is-complete + &.is-complete:after {
    width: 100%;
    }
}

.ProgressBar {
    margin: 0 auto;
    padding: 2em 0 3em;
    list-style: none;
    position: relative;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
}

.ProgressBar-step {
    text-align: center;
    position: relative;
    width: 100%;
}
.ProgressBar-step:before, .ProgressBar-step:after {
    content: "";
    height: 0.5em;
    background-color: #9F9FA3;
    position: absolute;
    z-index: 1;
    width: 100%;
    left: -50%;
    top: calc(50% - .25em);
    -webkit-transform: translateY(-50%);
    transform: translateY(-50%);
    -webkit-transition: all .25s ease-out;
    transition: all .25s ease-out;
}
.ProgressBar-step:first-child:before, .ProgressBar-step:first-child:after {
    display: none;
}
.ProgressBar-step:after {
    background-color: #00637C;
    width: 0%;
}
.ProgressBar-step.is-complete + .ProgressBar-step.is-current:after, .ProgressBar-step.is-complete + .ProgressBar-step.is-complete:after {
    width: 100%;
}

.ProgressBar-icon {
    width: 1.5em;
    height: 1.5em;
    background-color: #9F9FA3;
    fill: #9F9FA3;
    border-radius: 50%;
    padding: 0.5em;
    max-width: 100%;
    z-index: 10;
    position: relative;
    -webkit-transition: all .25s ease-out;
    transition: all .25s ease-out;
}
.is-current .ProgressBar-icon {
    fill: #00637C;
    background-color: #00637C;
}
.is-complete .ProgressBar-icon {
    fill: #DBF1FF;
    background-color: #00637C;
}

.ProgressBar-stepLabel {
    display: block;
    text-transform: uppercase;
    color: #9F9FA3;
    position: absolute;
    padding-top: 0.5em;
    width: 100%;
    -webkit-transition: all .25s ease-out;
    transition: all .25s ease-out;
}
.is-current > .ProgressBar-stepLabel, .is-complete > .ProgressBar-stepLabel {
    color: #00637C;
}

.wrapper {
    max-width: 1000px;
    margin: 4em auto;
    font-size: 16px;
}

/*FOR DESKTOP STYLES*/
@media only screen and (min-width: 768px) {
    .text {
        padding-left: 50px;
    }

}

0 个答案:

没有答案