gpt4 book ai didi

javascript - 记住 JavaScript 跨页幻灯片中的图像编号

转载 作者:行者123 更新时间:2023-12-02 18:04:51 32 4
gpt4 key购买 nike

我正在制作一个纯 html+JavaScript 幻灯片。幻灯片位于网站的侧边栏中,该网站为每个具有幻灯片侧边栏的页面加载了 php。唯一没有侧边栏的页面是主页。

幻灯片放映工作正常。然而,可以理解的是,每次我使用侧边栏进入新页面时,幻灯片都会重新开始。这是有道理的,因为每个新页面都会重新加载 JavaScript。

我想找到某种方法让幻灯片记住它的位置,这样当我进入新页面时,幻灯片就会从上一页中断的地方继续。我只能想到两种解决方案,一种看起来很暴力,另一种我不知道该怎么做:

  1. 将当前图像编号写入文件并在每次调用时读取它幻灯片加载。
  2. 不知何故使用ajax,但我还没学会使用ajax然而(它会起作用吗?)。

有什么建议吗?哦,拜托,我正在学习 javascript,接下来是 jQuery 和 ajax,但是...

这是我的代码:

simpleslideshow.html:

<html>

初始化SlideShow();

<table width="100">
<tr>
<td align="left"> <div id="previous"> <a href="javascript:change_image(-1)">Previous</a></div></td>
<td align="right"> <div id="next"><a href="javascript:change_image(1)">Next</a></div></td>
<td align="right"> <div id="auto"><a href="javascript:auto()">auto</a></div></td>
<td align="right"> <div id="stop"><a href="javascript:stop()">stop</a></div></td>
</tr>
</table>

<img src="" id="slideshow-image" width="400px" height="auto" style="display:block;"/>

simpleslideshow.js:

var inaterval_ID = 0;
var image_number = 0;
var num_images = images_with_captions.length;


function change_image(increment){
image_number = image_number + increment;
image_number = (image_number + num_images) % num_images;
var string = images_with_captions[image_number].source;
document.getElementById("slideshow-image").src = string;
}

function initializeSlideShow() {
//var string = images_with_captions[0].source;
//document.getElementById("slideshow-image").src = string;
auto();
}

function auto() {
interval_ID = setInterval("change_image(1)", 1000);
}

function stop() {
clearInterval(interval_ID);
}

image_caption_list.js:

var images_with_captions = new Array(
{
source: "http://www.picgifs.com/clip-art/flowers-and-plants/flowers/clip-art-flowers-435833.jpg",
caption: "flower 1"
},
{
source: "http://www.picgifs.com/clip-art/flowers-and-plants/flowers/clip-art-flowers-511058.jpg",
caption: "flower 2"
},
{
source: "http://www.picgifs.com/clip-art/flowers-and-plants/flowers/clip-art-flowers-380016.jpg",
caption: "flower 3"
}
);

编辑:我无法获得 jsfiddle上类。但是here是一个实时版本,可能会或可能不会一段时间:

最佳答案

每次更改到新幻灯片时,您都可以将当前幻灯片值保存到 cookie 或 localStorage,然后当您在新页面上启动幻灯片时,您可以读取上一张幻灯片值并从该幻灯片编号开始.

这里是阅读上一张幻灯片编号:

function initializeSlideShow() {
// get prior slideshow num
var lastSlideNum = +readCookie("lastSlideNum");
// if there was a prior slideshow num, set that as the last one we used
if (lastSlideNum) {
image_number = lastSlideNum;
}
auto();
}

这里是在每次更改时保存幻灯片编号:

function change_image(increment){
image_number = (image_number + increment) % num_images;
// remember what slide we're on for subsequent page loads
createCookie("lastSlideNum", image_number);
var string = images_with_captions[image_number].source;
document.getElementById("slideshow-image").src = string;
}

而且,这是一个简单的 cookie 库:

// createCookie()
// name and value are strings
// days is the number of days until cookie expiration
// path is optional and should start with a leading "/"
// and can limit which pages on your site can
// read the cookie.
// By default, all pages on the site can read
// the cookie if path is not specified
function createCookie(name, value, days, path) {
var date, expires = "";
path = path || "/";
if (days) {
date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires=" + date.toGMTString();
}
document.cookie = name + "=" + value + expires + "; path=" + path;
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function eraseCookie(name) {
createCookie(name, "", -1);
}
<小时/>

localStorage 实现(不会记住 IE8 之前的 IE 版本中的幻灯片)如下所示:

function initializeSlideShow() {
// get prior slideshow num
var lastSlideNum;
// localStorage requires IE8 or newer
// if no localStorage, then we just don't remember the previous slide number
if (window.localStorage) {
lastSlideNum = +localStorage["lastSlideNum"];
// if there was a prior slideshow num, set that as the last one we used
if (lastSlideNum) {
image_number = lastSlideNum;
}
auto();
}

这里是在每次更改时保存幻灯片编号:

function change_image(increment){
image_number = (image_number + increment) % num_images;
// remember what slide we're on for subsequent page loads
if (window.localStorage) {
localStorage["lastSlideNum"] = image_number;
}
var string = images_with_captions[image_number].source;
document.getElementById("slideshow-image").src = string;
}

关于javascript - 记住 JavaScript 跨页幻灯片中的图像编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20185284/

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