我正在构建这个网站,其中包含用javasript编写的图像幻灯片。这是相关的html。
<table>
<tr>
<td id="firstelement">
<div id="leftsidebar">
<img class="leftslide" src="/images/IMG_96768613163825.jpeg">
<img class="leftslide" src="/images/IMG_96772192197469.jpeg">
<img class="leftslide" src="/images/IMG_96781303842831.jpeg">
<img class="leftslide" src="/images/IMG_96785091848819.jpeg">
<img class="leftslide" src="/images/IMG_96764834500597.jpeg">
</div>
</td>
<td id="secondelement">
<div id="slideshow">
<img class="slide" src="/images/IMG_96764834500597.jpeg">
<img class="slide" src="/images/IMG_96768613163825.jpeg">
<img class="slide" src="/images/IMG_96772192197469.jpeg">
<img class="slide" src="/images/IMG_96781303842831.jpeg">
<img class="slide" src="/images/IMG_96785091848819.jpeg">
</div>
</td>
<td id="thirdelement">
<ul id="announcements"><h3>Announcements</h3>
<li class="announcement">Announcement #1 - Dates and times could go here, along with a short description of the announcement.</li><hr>
<li class="announcement">Announcement #2 - These could just be links to pages that have fliers or other representations of the announcements.</li><hr>
<li class="announcement">Announcement #3 - Could even add images, experiment with fonts, etc.</li><hr>
<li class="announcement">Announcement #4 - These would change as needed. I am only adding generic text because I am not aware of any current announcements that need posted.</li><hr>
</ul>
</td>
</tr>
</table>
这是使其运行的幻灯片.js
// Global variables
var numslides = 0;
var numleftslides = 0;
var currentslide = 0, oldslide = 4;
var slide1 = 0;
var slide2 = 1;
var slide3 = 2;
var slide4 = 3;
var slide5 = 4;
var x = 0;
var a = 0;
var b = 240;
var c = 480;
var d = 720;
var e = 960;
var picWidth = 0;
var leftPicHeight = 0;
var slides = new Array();
var leftslides = new Array();
function makeSlideShow() {
// hide the nav bar options
setupMenu();
// find all images with the class "slide"
imgs = document.getElementsByTagName("img");
for (i = 0; i < imgs.length; i++) {
if (imgs[i].className != "slide" && imgs[i].className != "leftslide") continue;
else if (imgs[i].className == "slide" ) {
slides[numslides] = imgs[i];
jQuery("#slideshow").css('width', jQuery(slides[numslides]).css('width'));
// stack images with first slide on top
if (numslides == 0) {
imgs[i].style.zIndex = 10;
}
else {
imgs[i].style.zIndex = 0;
}
//imgs[i].onclick = nextSlide;
numslides++;
}
else {
leftslides[numleftslides] = imgs[i];
numleftslides++;
}
} // end for loop
nextSlide();
} // end makeSlideShow()
function nextSlide() {
// Set current slide to be under the new top slide
slides[currentslide].style.zIndex = 9;
// Move older slide to the bottom of the stack
slides[oldslide].style.zIndex = 0;
oldslide = currentslide;
currentslide++;
if (currentslide >= numslides) currentslide = 0;
// start at the right edge
picWidth = parseInt(jQuery(slides[currentslide]).css('width'));
jQuery(slides[currentslide]).css('left', -picWidth + "px");
x = -picWidth;
// Move the new slide to the top
slides[currentslide].style.zIndex = 10;
if (a < 0) a = 960;
if (b < 0) b = 960;
if (c < 0) c = 960;
if (d < 0) d = 960;
if (e < 0) e = 960;
animateSlide();
window.setTimeout("nextSlide();", 5000);
}
function animateSlide() {
// Lower moves slower, higher moves faster
x = Math.min(0, x + 7);
a = Math.max(-240, a - 2);
b = Math.max(-240, b - 2);
c = Math.max(-240, c - 2);
d = Math.max(-240, d - 2);
e = Math.max(-240, e - 2);
// previous image moves off the left edge
// (comment the next line for a different effect)
jQuery(slides[oldslide]).css('left', (x + picWidth) + "px");
jQuery(slides[currentslide]).css('left', x + "px");
jQuery(leftslides[slide1]).css('top', a + "px");
jQuery(leftslides[slide2]).css('top', b + "px");
jQuery(leftslides[slide3]).css('top', c + "px");
jQuery(leftslides[slide4]).css('top', d + "px");
jQuery(leftslides[slide5]).css('top', e + "px");
// repeat until slide is at zero position
if (x < 0) window.setTimeout("animateSlide();", 1);
}
// create the slideshow when the page loads
jQuery(document).ready(makeSlideShow);
幻灯片实际上(有点)有效。在Firefox和Chrome中,当浏览器启动时,它似乎工作正常。然而,当我最小化它,然后再将它重新启动时,图片仍然有效,但是它们没有对齐。在Internet Explorer中,图像简单,移动速度不够快,时间也全部关闭。任何帮助将不胜感激。我是在网络比赛中进入这个网站,明天下午我会进行演示。提前谢谢。
答案 0 :(得分:1)
不是答案,但这对评论来说太长了:
有些事情你可以优化。例如。没有必要按标签名称获取所有图像,
然后使用for
- 循环检查每个图像是否具有类名slide
然后将其添加到数组中。由于您已经在使用jQuery,因此您只需获取所有内容即可
使用
var slides = jQuery.makeArray($(".slide"));
我刚刚将您的标记添加到Fiddle(没有您的脚本)并添加了一些虚拟图像和控制台日志消息,因此很容易检查此数组包含的内容。
答案 1 :(得分:1)
经过多次努力,我相信我知道问题是什么,我找到了解决问题的方法。我认为可能发生的事情是,改变页面聚焦和关闭会对幻灯片放映产生足够的干扰,使得图像定位的计算跟不上显示。当你这样做时,这几乎就像你陷入了“竞争状态”。所以我通过使用window.onfocus(...)
事件使用适当的函数触发器来重置所有内容。如果用户离开页面并返回,则允许重置所有内容,例如在最小化和恢复页面的情况下。由于IE没有足够快地加载图像,我放置了一个if-statement
来查看浏览器是否是IE,然后处理不同的事情。这似乎是一个很好的补丁。不幸的是,现在我意识到真正的大屏幕会扭曲网站并干扰某些功能。我想我现在必须接受这个,但如果有人对此有任何建议,我会全力以赴。感谢所有参与此问题的人。