얼렁뚱땅코드/jquery

swiper:사용자가 제작한 네비게이션을 클릭하면 이미지 바뀌게하기

얼렁뚱땅 디자이너 2019. 9. 27. 21:41

나는 mouseenter를 사용했는데 click을 사용해도 좋다.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="reset.css">
    <link rel="stylesheet" href="/css/swiper.css">
    <style>
        html,
        body {
            position: relative;
            height: 100%;
        }

        body {
            background: #eee;
            font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
            font-size: 14px;
            color: #000;
            margin: 0;
            padding: 0;
        }

        .swiper-container {
            width: 100%;
            height: auto;
        }

        .swiper-slide {
            text-align: center;
            font-size: 18px;
            background: #fff;
            /* Center slide text vertically */
            display: -webkit-box;
            display: -ms-flexbox;
            display: -webkit-flex;
            display: flex;
            -webkit-box-pack: center;
            -ms-flex-pack: center;
            -webkit-justify-content: center;
            justify-content: center;
            -webkit-box-align: center;
            -ms-flex-align: center;
            -webkit-align-items: center;
            align-items: center;
        }

        .swiper-pagination-bullet {
            width: 20px;
            height: 20px;
            text-align: center;
            line-height: 20px;
            font-size: 12px;
            color: #000;
            opacity: 1;
            background: rgba(0, 0, 0, 0.2);
        }

        .swiper-pagination-bullet-active {
            color: #fff;
            background: #007aff;
        }

        .nav {
            position: absolute;
            left: 10%;
            top: 25%;
            transform: translate(-25%);
            z-index: 999;
            width: 100px;
        }

        .nav a.ico {
            display: block;
            widows: 100px;
            height: 100px;
            background: yellowgreen;
        }

        .nav ul li {
            background: rgba(0, 0, 0, 0.5);
            text-align: center;
            color: #fff;
        }

        .nav ul li a {
            width: 100px;
            height: 100px;
            line-height: 100px;
            color: #fff;
            height: 100%;
            display: block;
        }

        .nav ul li.on a {
            color: skyblue;
        }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="/js/swiper.js"></script>
    <script>
        $(function () {
            //var d = new Date();
            //alert(d);

            var swiper = new Swiper('.swiper-container', {
                effect: 'fade',
                autoplay: {
                    delay: 2500,
                    disableOnInteraction: false,
                },
            });
            swiper.on('slideChange', function () {
                var i = swiper.activeIndex;

                $('.nav ul li').removeClass('on');//nav ul li를 지워라 'on'되어있는 것을
                $('.nav ul li').eq(i).addClass('on');//nav ul li들 중에서 i번째를 보여줘라
            });
            $('.nav ul li').mouseenter(function(){
                var idx = $(this).index()//현재 index순서를 
                swiper.slideTo(idx,1000,false);//swiper 슬라이드해라 (인덱스,1초마다,콜백안하고)
            });

        });
    </script>
</head>

<body>
    <!-- Swiper -->
    <div class="swiper-container">
        <div class="swiper-wrapper">
            <div class="swiper-slide"><img src="img/portfolio_1.jpg"></div>
            <div class="swiper-slide"><img src="img/portfolio_2.jpg"></div>
            <div class="swiper-slide"><img src="img/portfolio_3.jpg"></div>
            <div class="swiper-slide"><img src="img/portfolio_4.jpg"></div>
            <div class="swiper-slide"><img src="img/portfolio_5.jpg"></div>
        </div>
        <div class="nav">
            <a href="#" class="ico"><img src="img/ico_quick.png"></a>
            <ul>
                <li class="on"><a href="#">등기구</a></li>
                <li><a href="#">등주</a></li>
                <li><a href="#">경관조명</a></li>
                <li><a href="#">태양광</a></li>
                <li><a href="#">실내조명</a></li>
            </ul>
        </div>
</body>

</html>

개인적으로 스크립트 부분이 해석이 되지 않는데

swiper.on('slideChange', function () {
  var i = swiper.activeIndex;

  $('.nav ul li').removeClass('on'); //
  $('.nav ul li').eq(i).addClass('on'); //
});
$('.nav ul li').mouseenter(function(){
  var idx = $(this).index() //
  swiper.slideTo(idx,1000,false); //
});

이부분을 해석하는 것을 조금 더 공부해야 겠다.