JavaScript,丢失滚动事件

时间:2011-06-17 21:01:54

标签: javascript javascript-events scroll

我有html页面,其中三列调整到窗口的高度。每列都有自己的内容,可以滚动。因为整个页面的高度与窗口的高度相同,所以窗口的滚动事件不会触发。但是我也失去了每一列的滚动事件,他们也没有触发事件。

这个页面在jsFiddle(here you can check my source code)中完美运行的有趣事实。 我认为这是因为这项服务的框架性质。问题是如何在浏览器中捕获这些滚动事件?感谢。

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--?xml version="1.0" encoding="utf-8"?-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Read The Code</title>
    <link href="css/main.css" rel="stylesheet" type="text/css">
    <link href="css/code-ray.css" rel="stylesheet" type="text/css">
    <script src="js/jquery-1.6.1.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/jquery.scrollTo.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/main.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="outerDiv">
    <div id="leftDiv">
        <img src="images/Arrow-Down.png" id="leftArrow" alt="" title="">
        <div id="left-container">
            <div id="left">

            </div>
        </div>
    </div>
    <div id="centerDiv">
        <img src="images/Arrow-Down.png" id="centerArrow" alt="" title="">
        <div id="center-container">
            <div id="center">
<div class="CodeRay">
  <div class="code"><pre><span class="no">   1</span> require <span class="s"><span class="dl">'</span><span class="k">yaml</span><span class="dl">'</span></span>
<span class="no">   2</span> <span>require</span> <span class="s"><span class="dl">'</span><span class="k">set</span><span class="dl">'</span></span>
...
</pre></div>
</div>

            </div>
        </div>
    </div>
    <div id="rightDiv">
        <img src="images/Arrow-Down.png" id="rightArrow" alt="" title="">
        <div id="right-container">
            <div id="right">

            </div>
        </div>
    </div>
</div>
</body>
</html>

CSS:

html, body, div {
    margin: 0;
    border: 0 none;
    padding: 0;
}

html, body, #outerDiv, #left-container, #center-container, #right-container {
   height: 100%;
   min-height: 100%;
}

body {
    font-family: Georgia, serif;
}

#outerDiv {
    width: auto;
    margin: 0 auto;
    overflow: hidden;
}

#leftDiv {
    float: left;
    position: relative;
    width: 32.7%;
    height:100%;
}

#left-container {
    overflow: scroll;
    position: relative;
}

#left {
    display: inline-block;
    position: absolute;
    top: 0px;
}

#centerDiv {
    float: left;
    position: relative;
    width: 34.6%;
    height:100%;
}

#center-container {
    overflow: scroll;
    position: relative;
}

#center {
    display: inline-block;
    position: absolute;
    top: 0px;
}

#rightDiv {
    float: left;
    position: relative;
    width: 32.7%;
    height:100%;
}

#right-container {
    overflow: scroll;
    position: relative;
}

#right {
    display: inline-block;
    position: absolute;
    top: 0px;
}

.green_highlight {
    background-color: green;
}

img {
    position: absolute;
    width: 20px;
    height: 20px;
    z-index: 1;
    top: 0px;
    right: 15px;
}

img:hover {
    cursor: pointer
}

JavaScript的:

$(window).scroll(function() {
  alert("Scroll event from window");
});

$('#centerDiv').scroll(function() {
  alert("Scroll event from centerDiv");
});

$('#center-container').scroll(function() {
  alert("Scroll event from center-container");
});

$('#center').scroll(function() {
  alert("Scroll event from center");
});

2 个答案:

答案 0 :(得分:0)

这些都应该与jQuery一起使用。

听起来好像你可能不理解为什么它在jsFiddle中有效,这是因为它会自动包含jQuery

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>

将其放入<head>

答案 1 :(得分:0)

问题是因为事件处理程序在未初始化的文档上初始化。我只是将事件处理程序放在$(document).ready(function(){})中;解决这个问题。