얼렁뚱땅코드/jquery

좌우로 움직이는 슬라이드 만들기

얼렁뚱땅 디자이너 2019. 10. 29. 21:04
<!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>
</head>
<link rel="stylesheet" href="/reset.css">
<style>
    .bgcontainer{width:600px; height:400px; position: relative; border: 1px solid #000; margin:auto;}
    .bgwrap{width:400%; height:100%; display: flex; margin-left: -100%;}
    .bg{width: 100%;height: 100%;}
    .bg01{background: burlywood;}
    .bg02{background: blue;}
    .bg03{background: cadetblue}
    .bg04{background: crimson;}

    .btn{position: absolute; top:50%; transform: translateY(-50%);}
    a.prev{left: 0;}
    a.next{right: 0;}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
    $(function(){
        $('a.prev').click(function(){
            $('.bgwrap').animate({'margin-left':'0'},1000,function(){
                $('.bg').last().prependTo('.bgwrap')
                $('.bgwrap').css({'margin-left':'-100%'})
            })
        })
        $('a.next').click(function(){
            $('.bgwrap').animate({'margin-left':'-200%'},1000,function(){
                $('.bg').first().appendTo('.bgwrap')
                $('.bgwrap').css({'margin-left':'-100%'})
            })
        })                
    })
</script>
<body>
    <div class="bgcontainer">
        <div class="bgwrap">
            <div class="bg bg01"></div>
            <div class="bg bg02"></div>
            <div class="bg bg03"></div>
            <div class="bg bg04"></div>
        </div>
        <a href="#" class="prev btn"><img src="img/prev.png" alt=""/></a>
        <a href="#" class="next btn"><img src="img/next.png" alt=""/></a>
    </div>
</body>
</html>

스크립트 한번 더 정리!

<script>
    $(function(){
        $('a.prev').click(function(){
            $('.bgwrap').animate({'margin-left':'0'},1000,function(){
                $('.bg').last().prependTo('.bgwrap')
                $('.bgwrap').css({'margin-left':'-100%'})
            })
        })
        $('a.next').click(function(){
            $('.bgwrap').animate({'margin-left':'-200%'},1000,function(){
                $('.bg').first().appendTo('.bgwrap')
                $('.bgwrap').css({'margin-left':'-100%'})
            })
        })                
    })
</script>

 

응용하기!!

<script>
    $(function(){
        var w = $('.bg').width()
        $('a.prev').click(function(){
            $('.bgwrap').animate({'margin-left':'0'},1000,function(){
                $('.bg').last().prependTo('.bgwrap')
                $('.bgwrap').css({'margin-left':-w})
            })
        })
        $('a.next').click(function(){
            $('.bgwrap').animate({'margin-left':-w*2},1000,function(){
                $('.bg').first().appendTo('.bgwrap')
                $('.bgwrap').css({'margin-left':-w})
            })
        })                
    })
</script>

'얼렁뚱땅코드 > jquery' 카테고리의 다른 글

autoBannerslide  (0) 2019.10.31
가로세로 슬라이드  (0) 2019.10.29
이중 tab을 사용해서 움직이는 탭화면 만들기  (0) 2019.10.25
appendTo 슬라이드 만들기  (0) 2019.10.25
해양환경공단 샘플 테스트  (0) 2019.10.25