简体   繁体   English

如何为这个 Javascript 幻灯片添加淡入淡出效果?

[英]How do I add a fade effect to this Javascript slideshow?

The below image slideshow script works as intended in that it cycles through an array of images with an optional anchor link and description.下面的图像幻灯片脚本按预期工作,因为它循环显示带有可选锚链接和描述的图像数组。

Now I would like to improve this script by adding a fade in/out effect.现在我想通过添加淡入/淡出效果来改进这个脚本。 Any help is greatly appreciated.任何帮助是极大的赞赏。

Thank you in advance.先感谢您。

<script language="JavaScript1.2">
  var variableslide = new Array()

  variableslide[0] = ['image1.jpg', 'link here', 'description']
  variableslide[1] = ['image2.jpg', 'link here', 'description']
  variableslide[2] = ['image3.jpg', 'link here', 'description']

  var slidewidth = '600px'
  var slideheight = '250px'
  var slidebgcolor = '#FFFFFF'

  var slidedelay = 3000

  var ie = document.all
  var dom = document.getElementById

  for (i = 0; i < variableslide.length; i++) {
    var cacheimage = new Image()
    cacheimage.src = variableslide[i][0]
  }

  var currentslide = 0

  function rotateimages() {
    contentcontainer = '<center>'
    if (variableslide[currentslide][1] != "")
      contentcontainer += '<a href="' + variableslide[currentslide][1] + '">'
    contentcontainer += '<img src="' + variableslide[currentslide][0] + '" border="0" vspace="3">'
    if (variableslide[currentslide][1] != "")
      contentcontainer += '</a>'
    contentcontainer += '</center>'
    if (variableslide[currentslide][2] != "")
      contentcontainer += variableslide[currentslide][2]

    if (document.layers) {
      crossrotateobj.document.write(contentcontainer)
      crossrotateobj.document.close()
    } else if (ie || dom)
      crossrotateobj.innerHTML = contentcontainer
    if (currentslide == variableslide.length - 1) currentslide = 0
    else currentslide++
      setTimeout("rotateimages()", slidedelay)
  }

  if (ie || dom)
    document.write('<div id="slidedom" style="width:' + slidewidth + ';height:' + slideheight + ';     background-color:' + slidebgcolor + '"></div>')

  function start_slider() {
    crossrotateobj = dom ? document.getElementById("slidedom") : ie ? document.all.slidedom : document.slidensmain.document.slidenssub
    if (document.layers)
      document.slidensmain.visibility = "show"
    rotateimages()
  }

  if (ie || dom)
    start_slider()
  else if (document.layers)
    window.onload = start_slider
</script>
<ilayer id="slidensmain" width=&{slidewidth}; height=&{slideheight}; bgColor=&{slidebgcolor}; visibility=hide>
  <layer id="slidenssub" width=&{slidewidth}; left=0 top=0></layer>
</ilayer>

What you want to do is programatically decrease the opacity property of the image DOM object to fade out whilst simultaneously increasing the opacity of the image to fade in. You start the image to fade in at 0 opacity and work your way up.您想要做的是以编程方式降低图像 DOM 对象的opacity属性以淡出,同时增加图像的不透明度以淡入。您开始图像以 0 不透明度淡入并逐步增加。

I found an example for you using straight JavaScript with no jQUery or other frameworks based on your code style - see how you go with http://tech.pro/tutorial/725/javascript-tutorial-simple-fade-animation我为您找到了一个使用直接 JavaScript 的示例,不使用 jQUEry 或其他基于您的代码风格的框架 - 看看您如何使用http://tech.pro/tutorial/725/javascript-tutorial-simple-fade-animation

Can you use jQuery?你会使用 jQuery 吗? They already have fadeIn and fadeOut where you can set various properties, one being the duration in milliseconds (to determine how long it will last).它们已经有fadeIn 和fadeOut,您可以在其中设置各种属性,其中一个是以毫秒为单位的持续时间(以确定它将持续多长时间)。

http://api.jquery.com/fadeIn/ http://api.jquery.com/fadeIn/

If you must use plain JS then it gets a little more complicated.如果你必须使用普通的 JS 那么它会变得有点复杂。 Do you need to be compatible with older browsers?您需要与旧浏览器兼容吗?

The easiest way to do it would be to setInterval and then step down the opacity to 0 but not all browsers/versions have support for opacity.最简单的方法是设置间隔,然后将不透明度降低到 0,但并非所有浏览器/版本都支持不透明度。

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

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