개발

슬라이드쇼 예제 본문

Frontend/JavaScript

슬라이드쇼 예제

Study 2021. 3. 20. 15:27

 

 

현재 보고있는 사진 위치 디스플레이, 방향키로 사진 전환기능, 감속도 적용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.wrapper{
    width: 500px;
    height: 400px;
    background: yellow;
    margin: auto;
}
#photo-frame{
    width: 500px;
    height: 340px;
    background: blue;
    overflow: hidden;
    position: relative;
}
#container{
    width: 3500px;
    height: 340px;
}
#container img{
    float: left;
}
#navi{
    text-align: center;
    font-size:30px;
}
#navi a{
    text-decoration:none;
}
#navi a:hover{
    color: black;
}
#navi a:active{
    color: cornsilk;
}
</style>
<script>
var targetX=0;
var container; 
var a=0.1
var n1=0;
var n2=0;
function init(){
    container = document.getElementById("container");
    container.style.position="absolute"
    container.style.top=0+"px";
    container.style.left=0+"px";
    setInterval("move()"10);
}
function move(){ 
    container.style.left=parseFloat(container.style.left)+a*(targetX-parseFloat(container.style.left))+"px";
    n2=((targetX-500)*-1)/500
    //n2=(targetX*-1/500)+1 다른방법
    if(n1!=n2){
        document.getElementById("shu1").style.color="blue";
        document.getElementById("shu2").style.color="blue";
        document.getElementById("shu3").style.color="blue";
        document.getElementById("shu4").style.color="blue";
        document.getElementById("shu5").style.color="blue";
        document.getElementById("shu6").style.color="blue";
        document.getElementById("shu7").style.color="blue";
        document.getElementById("shu"+n2).style.color="pink";
        n1=n2
    }
}
function setTarget(x){
    targetX = x;
}
function joystick(){
    if(event.keyCode==37){
        if(targetX<0){
            targetX+=500;
        }
    }if(event.keyCode==39){
        if(targetX>-3000){
            targetX-=500;
        }
    }
}
window.addEventListener("load"function(){
    init();
});
</script>
</head>
<body onkeydown="joystick();">
    <div class="wrapper">
        <div id="photo-frame">
            <div id="container">
                <img src="images/shurek/img0.jpg">
                <img src="images/shurek/img1.jpg">
                <img src="images/shurek/img2.jpg">
                <img src="images/shurek/img3.jpg">
                <img src="images/shurek/img4.jpg">
                <img src="images/shurek/img5.jpg">
                <img src="images/shurek/img6.jpg">
            </div>
        </div>
        <div id="navi">
            <a href="javascript:setTarget(0)" id="shu1"></a>
            <a href="javascript:setTarget(-500);" id="shu2"></a>
            <a href="javascript:setTarget(-1000);" id="shu3"></a>
            <a href="javascript:setTarget(-1500);" id="shu4"></a>
            <a href="javascript:setTarget(-2000);" id="shu5"></a>
            <a href="javascript:setTarget(-2500);" id="shu6"></a>
            <a href="javascript:setTarget(-3000);" id="shu7"></a>
        </div>
    </div>
</body>
</html>
cs

'Frontend > JavaScript' 카테고리의 다른 글

랜덤 조 할당 예제 2  (0) 2021.03.20
랜덤으로 조 할당 예제  (0) 2021.03.20
카운트다운 예제  (0) 2021.03.20
스톱워치 예제  (0) 2021.03.20
함수의 매개변수와 리턴값  (0) 2021.03.20