我的页面上有一个JavaScript滑出式菜单。当页面加载时,菜单显示在错误的位置并处于打开状态(默认情况下应关闭)。一旦页面加载完毕,它就会跳转到正确的位置和状态。
我希望菜单要么最后加载,要么在开始时处于正确的位置。我曾尝试将菜单样式设置为隐藏,并在使用JavaScript加载页面时将其显示为一个块,但菜单一直处于隐藏状态。
<link rel="stylesheet" type="text/css" media="all" href="<css/style.css" />
<link rel="stylesheet" type="text/css" media="all" href="</css/reset.css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.tabSlideOut.v1.3.js"></script>
<script type="text/javascript" src="js/jquery.cycle.all.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.slide-out-div').tabSlideOut({
tabHandle: '.handle',
pathToTabImage: 'images/closed_menu.png', //class of the element that will become your tab
imageHeight: '230px', //height of tab image //Optionally can be set using css
imageWidth: '62px', //width of tab image //Optionally can be set using css
tabLocation: 'left', //side of screen where tab lives, top, right, bottom, or left
speed: 600, //speed of animation
action: 'hover', //options: 'click' or 'hover', action to trigger animation
topPos: '270px', //position from the top/ use if tabLocation is left or right
leftPos: '30px', //position from left/ use if tabLocation is bottom or top
fixedPosition: false //options: true makes it stick(fixed position) on scroll
});
$("div.slide-out-div").mouseover(function(){
$('.handle').css("background-image", "url(images/open_menu.png)");
}).mouseout(function(){
$('.handle').css("background-image", "url(images/closed_menu.png)");
});
$('#sliderimages').cycle({
fx: 'fade',
speed: 'fast',
timeout: 0,
next: '#next',
prev: '#prev'
});
});
</script>
<body onload="javascript:document.getElementByClass('slide-out-div').style.display = 'block';">然后是CSS
.slide-out-div {
display: none;
padding-left: 0px;
width: 200px;
z-index: 3;
position: relative;
}发布于 2011-09-19 04:34:55
一种可能的解决方案可能是这样。在html的<head>中添加:
<script>document.documentElement.className += ' js'</script>
这将在<html>元素上添加一个"js“类。
接下来,在css文件中像往常一样设置菜单样式,但紧接着添加.js类将其隐藏。例如:
.menu { /* your normal styling goes here */ }
.js .menu { display: none; }这确保了无论出于何种原因,您的菜单都显示在不支持JavaScript的客户端中,而隐藏在支持JavaScript的客户端中。
接下来,调整您的JavaScript文件,以便在需要时显示滑出菜单。
发布于 2011-09-19 15:15:19
今天早上,我以全新的眼光看待这个问题,我意识到我正在尝试使用Javascript中的一个类,而不是一个ID。
我从这里改变了
<body onload="javascript:document.getElementByClass('slide-out-div').style.display = 'block';">到这个
<body onload="javascript:document.getElementById('menu').style.display = 'block';">https://stackoverflow.com/questions/7464224
复制相似问题