简体   繁体   English

Javascript图像滑块转换

[英]Javascript image slider transition

I'm looking for a code, which adds a transition after the picture changes. 我正在寻找一个代码,它会在图片更改后添加一个转换。 Here's my code: 这是我的代码:

<script type = "text/javascript">
    function displayNextImage() {
        x = (x === images.length - 1) ? 0 : x + 1;
        document.getElementById("img").src = images[x];
    }

    function displayPreviousImage() {
        x = (x <= 0) ? images.length - 1 : x - 1;
        document.getElementById("img").src = images[x];
    }

    function startTimer() {
        setInterval(displayNextImage, 3000);
    }

var images = [], x = -1;
images[0] = "1.jpg";
images[1] = "2.jpg";
images[2] = "3.jpg";
</script>

<body onload = "startTimer()">
<img id="img" src="1.jpg"/>
</body>

Is there any possible way to add some transition when the code changes the image? 当代码更改图像时,是否有任何可能的方法来添加一些转换?

Roughly for example a fadeIn effect must be: 例如,fadeIn效果必须是:

function fadeIn(elementId, miliseconds) {
  var el = document.getElementById(elementId);
  el.style.opacity = 0;
  var op = parseFloat(0);

  var timer = setInterval(function() {
    if (op >= 1.0)
      clearInterval(timer);

    op += 0.1;
    el.style.opacity = op;
  }, miliseconds);
}

Here you have a working example with your code and some additions 这里有一个包含代码和一些附加功能的工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM