【发布时间】:2015-05-06 18:28:04
【问题描述】:
javascript 不是我的强项,因此我们将不胜感激任何正确方向的帮助或帮助!
基本上我有一个隐藏在我的页面上的 div,只有当他们是根据 cookie 设置的新访问者时才会显示给用户,但是它似乎在 firefox 和 chrome 中都运行良好,但在单击时使用 IE关闭按钮页面跳回顶部并显示隐藏的div(不应该,它应该保持隐藏)
HTML
<div id="theLink">
<?php if($this->countModules('tekenin2')) : ?>
<div id="gototop">
<div id="popup"><a href="#" onclick="parentNode.remove();return false; "><img src="/templates/marktoe/images/close.png" id="close" class="close" border="0" alt="close" /></a>
<jdoc:include type="modules" name="tekenin2" style="xhtml" />
</div>
</div>
<?php endif; ?>
脚本 1
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 setTheDivStyle() { // body on load event
if (!readCookie('visited')) {
// if cookie not found display the div and create the cookie
document.getElementById("theLink").style.display = "block";
createCookie('visited', 'visited', 1); // 1 day = 24 hours persistence
} else {
// if cookie found hide the div
document.getElementById("theLink").style.display = "none";
}
}
脚本 2 - 我怀疑问题出在哪里,但我可以确定
window.addEvent('domready',function() {
/* smooth */
new SmoothScroll({duration:500});
/* link management */
$('theLink').set('opacity','0').setStyle('display','block');
/* scrollspy instance */
var ss = new ScrollSpy({
min: 10,
onEnter: function(position,enters) {
//if(console) { console.log('Entered [' + enters + '] at: ' + position.x + ' / ' + position.y); }
$('theLink').fade('in');
},
onLeave: function(position,leaves) {
//if(console) { console.log('Left [' + leaves + '] at: ' + position.x + ' / ' + position.y); }
//value below was #gototop div
$('theLink').fade('out');
},
onTick: function(position,state,enters,leaves) {
//if(console) { console.log('Tick [' + enters + ', ' + leaves + '] at: ' + position.x + ' / ' + position.y); }
},
container: window
});
});
我需要的是要隐藏的 div,使用 cookie 检查用户是新用户还是返回用户,如果新则显示 div 否则隐藏它。
任何提示或建议将不胜感激,就像我说这不是我的强项,我觉得我缺少一些基本的东西。我浏览了多个其他线程,试图绕开它,但无济于事。 提前致谢!
【问题讨论】:
-
此外,当页面加载时,有时 div 会快速显示(在被脚本隐藏之前闪烁),但我不知道为什么。
-
关于闪现问题:您可以尝试默认隐藏它,仅在cookie不存在时才显示它。
-
确实,感谢您的回复,但是第一个sn-p中的最后一段代码不应该解决这个问题吗?
标签: javascript jquery css cookies jquery-cookie