English 中文(简体)
2. 移换发展要素
原标题:Moving elements on scroll

我只用 Java字和JQuery和I ve,用更简单的方式,在最近网上看到我所看到的影响。

What I am trying to do is have a div move horizontally when the user scrolls. My thought, for a simple prototype, involves watching the scrollbar position, and whenever it travels 1% of the page, move the div a certain increment.

//Script
var percent = Math.round((1/100)*document.height);
$(document).ready(function(){
    var boxpos = 10;

    $(window).scroll(function(){
        var pos = $(window).scrollTop();
        if(pos%percent==0){
            boxpos+=10;
            $("#box").css("{left:"+boxpos+";}");
        }
    });
});


//css
#content{width:100%; height:4000px;}
#res{position:fixed; left:20px;}
#box{position:fixed; width:20px; height:20px; left:10px; background-color:#F66;}

//html
<body>
<div id="content">
    <div id="box"><p>&nbsp;</p></div>
    <p id="res"></p>
</div>

</body>

One thing I ve been having problem with is that the value of percent never seems to be as it should, and most of the time is 0. I cannot understand why that should be.

I m a beginner to programming so any and all comments are welcome. Am I approaching this in a poor way?

Thanks!

Edit: 给大家一个更好的想法,了解我所谈论的内容:

最佳回答

引言

var percent = Math.round((1/100)*$(document).height());
$(document).ready(function(){
    var boxpos = 10;

    $(window).scroll(function(){
        var pos = $(document).scrollTop();
        if(pos%percent==0){
            boxpos+=10;
            $("#box").css("left", boxpos);
        }
    });
});
问题回答

暂无回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签