如何在页面刷新时使用Javascript切换图像?
假设我有两张图片:
我希望在刷新页面时将这些图像切换到locationA和locationB。
模拟:
- Page Refresh #1
<img id='locationA' src='ImageA.jpg'>
<img id='locationB ' src='ImageB.jpg'>
- Page Refresh #2
<img id='locationA' src='ImageB.jpg'>
<img id='locationB ' src='ImageA.jpg'>
- Page Refresh #3
<img id='locationA' src='ImageA.jpg'>
<img id='locationB ' src='ImageB.jpg'>
[更新#1]
我尝试了这个实现,但它不起作用。谁能告诉我这段代码有什么问题?
<html>
<head>
<script type="text/javascript">
var images = [];
images[0] = "I_am_Super_Magnet%21.jpg";
images[1] = "World_In_My_Hand%21.jpg";
var index = sessionStorage.getItem('index');
if(index) index = 0;
if(index==0)
{
document.getElementById("locationA").src=images[index];
document.getElementById("locationB").src=images[index+1];
index = index + 1;
}
else if(index==1)
{
document.getElementById("locationA").src=images[index];
document.getElementById("locationB").src=images[index-1];
index = index - 1;
}
sessionStorage.setItem('index', index);
</script>
</head>
<body>
<img id='locationA' src=''>
<img id='locationB' src=''>
</body>
</html>
[更新#2]
经过测试:
以下是代码:
<html>
<head>
<script type="text/javascript">
function switchImage()
{
var images = [];
images[0] = "I_am_Super_Magnet%21.jpg";
images[1] = "World_In_My_Hand%21.jpg";
var index = sessionStorage.getItem('index');
if(index == null) index = 0;//set index to zero if null
index = parseInt(index);// parse index to integer, because sessionStorage.getItem() return string data type.
if(index == 0)
{
document.getElementById("locationA").src=images[index];
document.getElementById("locationB").src=images[index+1];
index = index + 1;
}
else if(index == 1)
{
document.getElementById("locationA").src=images[index];
document.getElementById("locationB").src=images[index-1];
index = index - 1;
}
sessionStorage.setItem('index', index);
}
</script>
</head>
<body onload="switchImage()">
<img id='locationA' src='src_locationA'>
<img id='locationB' src='src_locationB'>
</body>
</html>
感谢Jack 线索!并感谢Jon Kartago Lamida 样品!
感谢。
答案 0 :(得分:0)
在Cookie中设置一个标志变量(检查Javascript Cookie SO问题以设置Cookie。)
每次页面加载时(在你的onload函数中)都要检查标志的值。
标记值将存储在您的cookie中。
希望这有帮助, Shoubhik
答案 1 :(得分:0)
我试着根据杰克的例子,使用localStorage。大多数现代浏览器都支持这一点我还没有这个代码,但或多或少应该是这样的。
<html>
<head>
<script type="text/javascript">
function init(){
var imageA = 'imageA.jpg';
var imageB = 'imageB.jpg';
// state maintaned using localStorage
var toggle = localStorage.getItem('toggle');
if(toggle) toggle = "A"; // if no state yet, initialize
if(toggle == "A"){
toggle = "B";
document.getElementById('locationA').src=imageA;
document.getElementById('locationB').src=imageB;
}else{
toggle = "A";
document.getElementById('locationA').src=imageB;
document.getElementById('locationB').src=imageA;
}
// put state back to local storage
localStorage.setItem('toggle', toggle);
}
</script>
</head>
<body onload="init()">
<img id='locationA' src=''>
<img id='locationB' src=''>
</body>
</html>
答案 2 :(得分:0)
根据Damien_The_Unbeliever评论我为自己的问题创建了这个答案帖。
这是我使用的最终解决方案。
[更新#3]
经过测试:
基本上代码仍然与Update#2相同,不同的是我使用cookie而不是sessionStorage
。这是完整的代码:
<html>
<head>
<script type="text/javascript">
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; 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);
}
function switchImage()
{
var images = [];
images[0] = "I_am_Super_Magnet%21.jpg";
images[1] = "World_In_My_Hand%21.jpg";
var index = readCookie('index'); //sessionStorage.getItem('index');
if(index == null) index = 0;//set index to zero if null
index = parseInt(index);// parse index to integer, because sessionStorage.getItem() return string data type.
if(index == 0)
{
document.getElementById("locationA").src=images[index];
document.getElementById("locationB").src=images[index+1];
index = index + 1;
}
else if(index == 1)
{
document.getElementById("locationA").src=images[index];
document.getElementById("locationB").src=images[index-1];
index = index - 1;
}
createCookie('index', index); //sessionStorage.setItem('index', index);
}
</script>
</head>
<body onload="switchImage()">
<img id='locationA' src='src_locationA'>
<img id='locationB' src='src_locationB'>
</body>
</html>