image slider onclick button - JavaScript

♀尐吖头ヾ 提交于 2021-02-11 14:12:15

问题


I completely stuck with slider with url links inside the Array. How to make this code run appropriate? This is it: https://codepen.io/konradszymanski/pen/VwvLgYG?editors=0110

or here is html:

<script src='https://kit.fontawesome.com/a076d05399.js'></script>
  <div class="container">
    <button class="left" onclick="" ><i class='fas fa-angle-left'></i></button>
    <button class="right" onclick="next()"><i class='fas fa-angle-right'></i></button>
  </div>

and js:

 var images  = [{img: "https://upload.wikimedia.org/wikipedia/commons/5/50/Bodiam-castle-10My8-1197.jpg",},{img: "https:upload.wikimedia.org/wikipedia/commons/4/4d/Balmoral_Castle.jpg",}];
var x = images;
var element = document.getElementById("container");
element.innerHtml = x;
next();
function next(){
element.innerHTML = x;
};


回答1:


 var images  = [{img: "https://upload.wikimedia.org/wikipedia/commons/5/50/Bodiam-castle-10My8-1197.jpg"},{img: "https://upload.wikimedia.org/wikipedia/commons/4/4d/Balmoral_Castle.jpg"}];
var element = document.getElementById("container");
function next(direction){
  var index= element.dataset.index;
  if(direction=="right"){index++;}else{index--}
  if(index>images.length-1){index=0;}
  if(index<0){index=images.length-1;}
  element.dataset.index=index;
  element.style.backgroundImage  ="url('"+ images[index].img +"')";
};
*{
}
body{
  background-color: #3a4a63;
}
 img{
    min-width: 500px;
    height: 400px;  
  }
.container{
  min-width: 500px;
  height: 400px;
  margin: 0 auto;
  background-color:#363537;
  display: flex;
  justify-content:space-between;
  align-items: center;
  flex-direction: row;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-right: -50%;
  transform: translate(-50%, -50%);
  border: 7px solid #fff;
  background-image: url(https://upload.wikimedia.org/wikipedia/commons/5/50/Bodiam-castle-10My8-1197.jpg);
  background-size: cover;
  background-position:center center;
  background-repeat:no-repeat
}
button{
  font-size: 50px;
  padding: 5px 10px;
  background-color: #3a4a63;
  border: 4px solid #fff;
  border-radius: 10px;
}
.fa-angle-left,
.fa-angle-right{
  color: #fff;
}
.left{
  position: relative;
  left: -25px;
}
.right{
  position: relative;
  left: 25px;
}
*/
<script src='https://kit.fontawesome.com/a076d05399.js'></script>
  <div class="container"id="container"data-index="0">
    <button class="left"  onclick="next('left')"><i class='fas fa-angle-left'></i></button>
    <button class="right" onclick="next('right')"><i class='fas fa-angle-right'></i></button>
  </div>


来源:https://stackoverflow.com/questions/61205573/image-slider-onclick-button-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!