www.webdeveloper.com
Results 1 to 3 of 3

Thread: scroll a small div along the y direction of the page

  1. #1
    Join Date
    Nov 2013
    Posts
    44

    scroll a small div along the y direction of the page

    want to make the small div to move with the page while it is being scrolled.seems codes not working .please help me on this.
    Code:
    
    
    
    
    
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    
    <title>Untitled Document</title>
    <style>
    .mid{
    	height:2900px;
    	width:400px;
    	border:1px solid black;
    	margin:0 auto;
    }
    #od{
    	float:left;
    	height:300px;
    	width:150px;
    	border:1px solid red;
    	margin-left:143px;
    	top:20px;
    	position:relative;
    }
    </style>
    <script>
    window.onscroll=function(){setTimeout(movediv,300);}
    </script>
    
    </head>
    
    <body style="position:relative;">
    
    
    <div id="od"></div>
    <div class="mid"></div>
    <script>
    function movediv(){
    	var this_id=document.getElementById("od");
    	var heights=this_id.style.height;
    	var heights_num=heights.slice(0,-1);
    	var h=parseInt( heights_num);
    	var page_height=parseInt(window.pageYOffset);
    	if(page_height>h){
    		move();
    	}
    }
    function move(){
    	var m=document.getElementById("od");
    	var tops=m.style.top;
    	var d=parseInt(window.pageYOffset);
    	var ts=tops.slice(0,-1);
    	var t=parseInt(ts);
    	t++;
    	m.style.top = t +"px";
    	
    	var p = setTimeout(move,12);
    	if(t>d+60){
    		clearTimeout(p);
    		m.style.top = t +"px";
    	}
    	
    }
    
    </script>
    </body>
    </html>

  2. #2
    Join Date
    Oct 2013
    Posts
    483
    Code:
    #od{
    	float:left;
    	height:300px;
    	width:150px;
    	border:1px solid red;
    	margin-left:143px;
    	top:20px;
    	position:fixed;
    }

  3. #3
    Join Date
    Nov 2013
    Posts
    44
    thanks!
    but what about the javascript?i want it to delay after i scroll and then it will move downword??

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles