加载时分配错误的位置

时间:2011-09-18 20:25:22

标签: javascript html

我的页面上有一个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;
}

2 个答案:

答案 0 :(得分:0)

可能的解决方案可能是这个。在你的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文件以在需要时显示滑出式菜单。

答案 1 :(得分:0)

今天早上用新鲜的眼睛看着这个,刚刚意识到我正在尝试在我的Javascript中使用一个类而不是一个ID。

我改变了这个

<body onload="javascript:document.getElementByClass('slide-out-div').style.display = 'block';">

到这个

<body onload="javascript:document.getElementById('menu').style.display = 'block';">
相关问题