- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- <!--设置css样式-->
- <style>
- body{
- margin: 0 0 0 0;
- height: 1000px;
- width: 100%;
- }
- .d1{
- position: absolute;
- width: 100%;
- height: 500px;
- top: 50%;
- margin-top: -250px;
- }
- .d2{
- margin-left: 950px;
- }
- .d1 img{
- margin-left: 50px;
- position: relative;
- }
- .c1{
- display: block;
- padding-left: 500px;
- }
- </style>
-
- <script src="jQuery.js"></script>
- </head>
- <body>
- <div class="d1">
- <img src="https://dummyimage.com/900x400/0000ff/ff" alt="" class="c1 c2">
- <div class="d2">
- <input type="button" value="<-" id="b1">
- <input type="button" value="->" id="b2">
- </div>
- </div>
- <script>
- var imgList=['https://dummyimage.com/900x400/0000ff/ff','https://dummyimage.com/900x400/00/ff','https://dummyimage.com/900x400/ff0000/ff'];
- var $imgEle=$('img');
- var nowSrc=imgList.indexOf($imgEle[0].src);
- var $b1Ele=$('#b1');
- var $b2Ele=$('#b2');
- function f1(){
- $imgEle.fadeOut(500);
- console.log(nowSrc);
- if(nowSrc===0){
- nowSrc=imgList.length-1;
- }
- else {
- nowSrc--;
- }
- t=setTimeout(function () {
- $imgEle.attr('src',imgList[nowSrc]);
- $imgEle.fadeIn(500);
- },500);
- }
- function f2(){
- $imgEle.fadeOut(500);
- console.log(nowSrc);
- if(nowSrc===imgList.length-1){
- nowSrc=0;
- }
- else {
- nowSrc++;
- }
- t2=setTimeout(function () {
- $imgEle.attr('src',imgList[nowSrc]);
- $imgEle.fadeIn(500);
- },500);
- t2=null
- }
- $b1Ele.on('click',f1);
- $b2Ele.on('click',f2);
- </script>
- </body>
- </html>