2013-04-24 110 views
0

我想弄清楚如何让每个人每次进入我的网站时显示一次某个div。我已经看到了使用JavaScript和PHP做这件事的两种方式,但没有成功。需要一个div,每个会话只显示一次

这是我的代码,我想(如果可能的话)#spinner div(以及其中的所有内容)在每个会话中显示一次。

<body> 
****This part would only be displayed once**** 
<div id="spinner" style="display: none;"> 
<div class="sketch"> 
<canvas id="canvas" width="1280" height="224"></canvas> 
<script>var canvas = document.getElementById('canvas') 
    , context = canvas.getContext('2d') 
    , img = new Image() 
    , w 
    , h 
    , offset 
    , glitchInterval; 

img.src = 'http://www.bccustommade.com/images/loading.png'; 
img.onload = function() { 
    init(); 
window.onresize = init; 
}; 

var init = function() { 
clearInterval(glitchInterval); 
canvas.width = w = window.innerWidth; 
offset = w * .1; 
canvas.height = h = ~~(175 * ((w - (offset * 2))/img.width)); 
glitchInterval = setInterval(function() { 
    clear(); 
    context.drawImage(img, 0, 110, img.width, 175, offset, 0, w - (offset * 2), h); 
    setTimeout(glitchImg, randInt(250, 1000)); 
}, 500); 
}; 

var clear = function() { 
context.rect(0, 0, w, h); 
context.fill(); 
}; 

var glitchImg = function() { 
for (var i = 0; i < randInt(1, 13); i++) { 
    var x = Math.random() * w; 
    var y = Math.random() * h; 
    var spliceWidth = w - x; 
    var spliceHeight = randInt(5, h/3); 
    context.drawImage(canvas, 0, y, spliceWidth, spliceHeight, x, y, spliceWidth, spliceHeight); 
    context.drawImage(canvas, spliceWidth, y, x, spliceHeight, 0, y, x, spliceHeight); 
} 
}; 

var randInt = function(a, b) { 
return ~~(Math.random() * (b - a) + a); 
};</script> 
</div> 
<div class="loading"> 
    <div class="loading-dot"></div> 
    <div class="loading-dot"></div> 
    <div class="loading-dot"></div> 
<div class="loading-dot"></div> 
</div> 
</div> 
****This part would only be displayed once**** 

+0

你可以缩短代码的墙到相关的东西,你试过什么? – 2013-04-24 07:04:09

+0

我会给我一秒,我已经尝试了一些PHP教程,但没有工作 – Crazycriss 2013-04-24 07:05:41

+0

你看看https://developer.mozilla.org/en-US/docs/DOM/Storage – 2013-04-24 07:16:30

回答

0

你需要通过JavaScript调用这个PHP脚本或阅读PHP瓦尔在JavaScript直接在此页面,但我推荐某种后台调用。

<?php 
session_start(); 

if(!isset($_SESSION['spinner']) { 
    // Render content 
} 

// Continue with page or other sutff 
?> 
+0

谢谢,我会尽快尝试这个:) – Crazycriss 2013-04-24 07:41:27

0

写这些内容,直到一些会话具有值为true。一旦显示,将会话值设置为true。

我不熟悉PHP语法,但页面逻辑会这样。

if(session contains key "first_time" && session["first_time"] == false){ 

print "your code here"; 
// set session value to true 
session["first_time"] = true; 

} 

如果你想这是在所有页面上显示,包括在每一个PHP页面