通过纯javascript流畅地改变图像

change images smoothly by pure javascript

本文关键字:改变 图像 javascript      更新时间:2023-09-26

我想在不使用JQuery的情况下流畅地改变背景图像,因为我对JQuery一无所知,而且我想通过使用javascript来学习它。所以,我只通过javascript在1秒内完成了图像变化,但我不明白我能做些什么,这样图像就会平滑地变暗,滑动等。这是我的代码

        var index = 0,
          container = document.getElementById("Imagebackground");
        function autochange() {
          var image = ['http://placekitten.com/1000/600', 'http://placekitten.com/1024/620', 'http://placekitten.com/960/600'];
          container.style.backgroundImage = 'url(' + image[index++] + ')';
          if (index > 2) {
            index = 0;
          }
        }
        window.setInterval(autochange, 1000);
#Imagebackground {
  width: 100vw;
  background-image: url("http://placekitten.com/1000/590");
  background-size: cover;
  height: 80vh;
}
<div id="Imagebackground"></div>

试试这个,这是纯javascript代码

var myIndex = 0;
carousel();
function carousel() {
    var i;
    var x = document.getElementsByClassName("slides");
    for (i = 0; i < x.length; i++) {
       x[i].style.display = "none";
    }
    myIndex++;
    if (myIndex > x.length) {myIndex = 1}
    x[myIndex-1].style.display = "block";
    setTimeout(carousel, 3000); 
}
<div class="bannar">
    <img class="slides" src="http://css3.bradshawenterprises.com/images/Windows%20Logo.jpg" />
    <img class="slides" src="http://css3.bradshawenterprises.com/images/Turtle.jpg" />
    <img class="slides" src="http://css3.bradshawenterprises.com/images/Birdman.jpg" />
</div>

这里是你想要滑动的所有图像的位置

<div class="bannar">
    <img class="slides" src="images/bannar1.jpg">
    <img class="slides" src="images/bannar2.jpg">
    <img class="slides" src="images/bannar3.jpg">
</div

使用CSS过渡。不需要javascript

如果您需要对这段代码进行进一步的解释,可以在Demo 3中找到它:http://css3.bradshawenterprises.com/cfimg/

@-webkit-keyframes cf4FadeInOut {
 0% {
   opacity:1;
 }
 17% {
   opacity:1;
 }
 25% {
   opacity:0;
 }
 92% {
   opacity:0;
 }
 100% {
   opacity:1;
 }
}
@-moz-keyframes cf4FadeInOut {
 0% {
   opacity:1;
 }
 17% {
   opacity:1;
 }
 25% {
   opacity:0;
 }
 92% {
   opacity:0;
 }
 100% {
   opacity:1;
 }
}
@-o-keyframes cf4FadeInOut {
 0% {
   opacity:1;
 }
 17% {
   opacity:1;
 }
 25% {
   opacity:0;
 }
 92% {
   opacity:0;
 }
 100% {
   opacity:1;
 }
}
@keyframes cf4FadeInOut {
 0% {
   opacity:1;
 }
 17% {
   opacity:1;
 }
 25% {
   opacity:0;
 }
 92% {
   opacity:0;
 }
 100% {
   opacity:1;
 }
}
#example {
  position:relative;
  height:281px;
  width:450px;
  margin:0 auto;
}
#example img {
  position:absolute;
  left:0;
}
#example img {
  -webkit-animation-name: cf4FadeInOut;
  -webkit-animation-timing-function: ease-in-out;
  -webkit-animation-iteration-count: infinite;
  -webkit-animation-duration: 8s;
  -moz-animation-name: cf4FadeInOut;
  -moz-animation-timing-function: ease-in-out;
  -moz-animation-iteration-count: infinite;
  -moz-animation-duration: 8s;
  -o-animation-name: cf4FadeInOut;
  -o-animation-timing-function: ease-in-out;
  -o-animation-iteration-count: infinite;
  -o-animation-duration: 8s;
  animation-name: cf4FadeInOut;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
  animation-duration: 8s;
}
#example img:nth-of-type(1) {
  -webkit-animation-delay: 6s;
  -moz-animation-delay: 6s;
  -o-animation-delay: 6s;
  animation-delay: 6s;
}
#example img:nth-of-type(2) {
  -webkit-animation-delay: 4s;
  -moz-animation-delay: 4s;
  -o-animation-delay: 4s;
  animation-delay: 4s;
}
#example img:nth-of-type(3) {
  -webkit-animation-delay: 2s;
  -moz-animation-delay: 2s;
  -o-animation-delay: 2s;
  animation-delay: 2s;
}
#example img:nth-of-type(4) {
  -webkit-animation-delay: 0;
  -moz-animation-delay: 0;
  -o-animation-delay: 0;
  animation-delay: 0;
}
<div id="example">
  <img class="" src="https://via.placeholder.com/150/0000FF/808080" />
  <img class="" src="https://via.placeholder.com/150/000000/808080" />
  <img class="" src="https://via.placeholder.com/150/00FFFF/808080" />
  <img class="" src="https://via.placeholder.com/150/FF00FF/808080" />
</div>

你可以直接添加(.style。transition = "…")在函数内使用JavaScript,它可以正常工作