我正在构建这个网站,其中包含用 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>
这是让它运行的 slideshow.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 中,简单的图像移动速度不够快,并且计时不准。任何帮助将不胜感激。我要参加这个网站参加网络竞赛,明天下午我会进行演示。提前致谢。
最佳答案
不是答案,但这对于评论来说太长了:
有些事情你可以优化。例如。不需要通过标签名称获取所有图像,
然后使用 for
循环检查每个图像是否具有类名 slide
然后将其添加到数组中。因为你已经在使用 jQuery,你可以只获取所有
使用
var slides = jQuery.makeArray($(".slide"));
我刚刚将您的标记添加到 Fiddle (没有您的脚本)并添加了一些虚拟图像和控制台日志消息,因此很容易检查此数组包含的内容。
关于javascript - 尝试编写一个 javascript 幻灯片,但图像在一段时间后不对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27304464/