原生js伦比哦

来源:互联网 发布:px4数据更新 编辑:程序博客网 时间:2024/06/05 04:01

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style>
    *{
        margin:0;
        padding:0;
        }
    div{
        width:520px;
        height:280px;
        position:relative;
        background:pink;
        margin:0 auto;
        overflow:hidden;
        }
    div ul{
        width:3640px;
        position:absolute;
        left:-520px;
        }   
    div ul li{
        list-style:none;
        float:left;
        width:520px;
        height:280px;
        font-size:30px;
        text-align:center;
        line-height:280px;
        }
    .prev ,.next{
        width:30px;
        height:50px;
        position:absolute;
        background:rgba(0,0,0,.4);
        top:115px;
        }   
    .prev:hover,.next:hover{
        background:rgba(0,0,0,.6);
        }   
    .prev{
        left:0;
        }
    .next{
        right:0;
        }   
    .prev:after{
        content:'<';
        color:#fff;
        line-height:50px;
        text-align:center;
        width:100%;
        height:100%;
        display:inline-block;
        }   
    .next:after{
        content:'>';
        color:#fff;
        line-height:50px;
        text-align:center;
        width:100%;
        height:100%;
        display:inline-block;
        }
    div ol{
        position:absolute;
        bottom:10px;
        left:50%;
        margin-left:-50px;
        background:rgba(255,255,255,.2);
        }
    div ol li{
        list-style:none;
        float:left;
        color:#fff;
        width:10px;
        height:10px;
        border-radius:100%;
        background-color:#fff;
        overflow:hidden;
        text-indent:10000px;
        margin:0 5px;
        cursor:pointer;
        }
    .orange{
        background-color:orange!important;
        }           
                            
</style>
</head>
<body>
    <div id="box">
        <ul>
            <li><img src="https://img.alicdn.com/simba/img/TB1SYOGggLD8KJjSszeSuuGRpXa.jpg" /></li>
            <li><img src="https://aecpm.alicdn.com/tfscom/TB1eCWYbS_I8KJjy0FoXXaFnVXa.jpg" /></li>
            <li><img src="https://img.alicdn.com/tfs/TB1Pp2IfDnI8KJjy0FfXXcdoVXa-520-280.png_q90_.webp" /></li>
            <li><img src="https://img.alicdn.com/simba/img/TB1gWrUXfTM8KJjSZFDSuultXXa.jpg" /></li>
            <li><img src="https://img.alicdn.com/simba/img/TB10TmIgcnI8KJjSsziSuv8QpXa.jpg" /></li>
            <li><img src="https://img.alicdn.com/simba/img/TB1SYOGggLD8KJjSszeSuuGRpXa.jpg" /></li>
            <li><img src="https://aecpm.alicdn.com/tfscom/TB1eCWYbS_I8KJjy0FoXXaFnVXa.jpg" /></li>
        </ul>
        <a class="prev" href="javascript:void(0)"></a>
         <a class="next"href="javascript:void(0)"></a>
         <ol>
            <li class="orange">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
         </ol>
    </div>
</body>
</html>
<script>    
    var ul=document.querySelector("#box ul");
    var currentValue=Number(getStyle(ul,"left").replace("px",""));
    var li =document.querySelectorAll("#box ul li");
    var width=Number(getStyle(li[0],"width").replace("px",""));
    var prev=document.querySelector(".prev");
    var next=document.querySelector(".next");
    var ol=document.querySelector("#box ol");
    var oli=document.querySelectorAll("#box ol li");
    function Move(obj){
        var _this=this;
        this.decoration=obj.decoration||null;
        this.count=0;
        this.init=function(changeValue){
            ul.style.left=changeValue+"px";
            };
        this.action=function(){
                var timer=setInterval(function(){
                    if(_this.decoration=="left"){
                            _this.count-=10;
                            currentValue-=10;   
                        }else if(_this.decoration=="right"){
                            _this.count+=10;
                            currentValue+=10;   
                        }
                    if(currentValue==0){
                        currentValue=-width*(li.length-2);
                    }
                    if(currentValue==(li.length-1)*-width){
                        currentValue=-width;
                        }   
                    var index=Math.abs(currentValue/width)-1;
                    if(index>=li.length-(2+1)){
                        index=parseInt(index);
                        }
                    _this.olmove(Math.ceil(index)); 
                    _this.init(currentValue);
                    if(Math.abs(_this.count)==width){
                        clearInterval(timer);
                        }
                    },1)
                    
            };
        this.olmove=function(index){
            for(var j=0;j<oli.length;j++){
                oli[j].className="";
            }
            oli[Math.ceil(index)].className="orange";
            };
        this.olclick=function(){
            var arrays=new Array(oli.length);
            for(var i=0;i<oli.length;i++){
                arrays[i]=-(width*(i+1));
                 (function(i){
                     oli[i].onclick=function(){
                         _this.olmove(i);
                        var timer=setInterval(function(){
                            
                            if(arrays[i]<currentValue){
                                    if(currentValue-arrays[i]>width){
                                        currentValue-=30;
                                    }else{
                                        currentValue-=10;
                                        }
                                }else if(arrays[i]>currentValue){
                                    if(arrays[i]-currentValue>width){
                                        currentValue+=30;
                                    }else{
                                        currentValue+=10;
                                        }
                                }
                            if(arrays[i]==currentValue){
                                clearInterval(timer)
                                }   
                            _this.init(currentValue);
                            },1);
                         }
                     }(i));
                }
            };          
    }
        
    function getStyle(element,attr){
        if(element.currentstyle){
            return element.currentstyle[attr];
            }else{
                return window.getComputedStyle(element,null)[attr];
            }
        }
    prev.onclick=function(){
            var obj={
                decoration:"right",
            }
        run(obj)
        }       
    next.onclick=function(){
            var  obj={
                decoration:"left",
            }
            run(obj)
        }
                
    function run(obj){
         var move=new Move(obj);    
         move.action(); 
        }   
         var  obj={}
         var move=new Move(obj);
         move.olclick();
    
    
        
</script>