gpt4 book ai didi

javascript - 如何在 Javascript 中自动化工作幻灯片

转载 作者:太空宇宙 更新时间:2023-11-03 17:54:50 24 4
gpt4 key购买 nike

我正在尝试使用纯 Javascript 和 CSS 制作的幻灯片。目前脚本在点击时运行。我还想通过自动化来添加运行它的附加功能。我该怎么做。下面是 fiddle 的链接,它在点击时起作用。
Working onclick

var images = document.getElementById('slideshow-wrapper');
var Slideshow = {
Utils: {
siblings: function(element) {
var parent = element.parentNode,
childs = parent.childNodes,
sibls = [],
len = childs.length,
i;
for (i = 0; i < len; i++) {
var child = childs[i];
if (child.nodeType == 1 && child.tagName.toLowerCase() == 'img' && child !== element) {
sibls.push(child);
}
}
return sibls;
},
hideAll: function(elements) {
var len = elements.length,
i;
for (i = 0; i < len; i++) {
var element = elements[i];
element.className = 'hide';

}
},
show: function(element) {
element.className = 'show';
}
},
core: {
displayNavigation: function() {
var images = document.getElementById('slideshow-wrapper').getElementsByTagName('img'),
len = images.length,
i, nav = document.getElementById('slideshow-nav'),
html = '';
for (i = 0; i < len; i++) {
html += '<a href="#" data-img="' + i + '">' + (i + 1) + '</a>';
}
nav.innerHTML = html;
},
navigate: function() {
var links = document.getElementById('slideshow-nav').getElementsByTagName('a'),
len = links.length,
i;
for (i = 0; i < len; i++) {
var a = links[i];
a.onclick = function() {
var $i = this.getAttribute('data-img');
var img = document.getElementById('slideshow-wrapper').getElementsByTagName('img')[$i];
Slideshow.Utils.show(img);
var siblings = Slideshow.Utils.siblings(img);
Slideshow.Utils.hideAll(siblings);
return false;
};
}
}
},
init: function() {
for (var prop in this.core) {
if (typeof this.core[prop] === 'function') {
this.core[prop]();
}
}
}
};
Slideshow.init();
console.log(Slideshow);

我尝试在使用 setInterval 编写函数的核心对象下添加另一个函数,但无法获取它。

最佳答案

第 1 步:将以下内容插入 <head>您页面的一部分或单独的 js 文件

 <script>
<!--
var slideimages=new Array()
var slidelinks=new Array()
function slideshowimages(){
for (i=0;i<slideshowimages.arguments.length;i++){
slideimages[i]=new Image()
slideimages[i].src=slideshowimages.arguments[i]
}
}

function slideshowlinks(){
for (i=0;i<slideshowlinks.arguments.length;i++)
slidelinks[i]=slideshowlinks.arguments[i]
}

function gotoshow(){
if (!window.winslide||winslide.closed)
winslide=window.open(slidelinks[whichlink])
else
winslide.location=slidelinks[whichlink]
winslide.focus()
}

//-->
</script>

将以下内容插入 <body>您希望幻灯片放映的页面部分:

<a href="javascript:gotoshow()"><img src="FirstImage.jpg" name="slide" border=0 width=300 height=375></a>
<script>
<!--

//configure the paths of the images
slideshowimages("food1.jpg","food2.jpg","food3.jpg","food4.jpg","food5.jpg")

//configure the speed of the slideshow, in miliseconds
var slideshowspeed=2000

var whichlink=0
var whichimage=0
function slideit(){
if (!document.images)
return
document.images.slide.src=slideimages[whichimage].src
whichlink=whichimage
if (whichimage<slideimages.length-1)
whichimage++
else
whichimage=0
setTimeout("slideit()",slideshowspeed)
}
slideit()

//-->
</script>

这对你来说应该足够了,只需改变你的链接

关于javascript - 如何在 Javascript 中自动化工作幻灯片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26578147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com