473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

The Use of ScrollBy

Hello JavaScript experts,
I have a floating div which I drag all over the page. If the page has
scrollbars and users drag the floating div to the very top, page
scrolls up too. The problem occurs at this moment. As page scrolls up,
the div gets detached from the mouse while dragging.
How can I keep the floating div to always stay attached to the mouse
during a dragging process and scrolling the page?
Plug and Play code is a good example of my problem:

<html><head>
<style type="text/css">

..drag{
position:relative;
cursor:move;
z-index: 100;

}

</style>

<script type="text/javascript">

var dragobject={
z: 0, x: 0, y: 0, offsetx : null, offsety : null, targetobj : null,
dragapproved : 0,
initialize:function(){
document.onmousedown=this.drag
document.onmouseup=function(){this.dragapproved=0}
},

drag:function(e){
var evtobj=window.event? window.event : e
this.targetobj=window.event? event.srcElement : e.target
if (this.targetobj.className=="drag"){
this.dragapproved=1
if
(isNaN(parseInt(this.targetobj.style.left))){this. targetobj.style.left=0}
if
(isNaN(parseInt(this.targetobj.style.top))){this.t argetobj.style.top=0}
this.offsetx=parseInt(this.targetobj.style.left)
this.offsety=parseInt(this.targetobj.style.top)
this.x=evtobj.clientX
this.y=evtobj.clientY
if (evtobj.preventDefault)
evtobj.preventDefault()
document.onmousemove=dragobject.moveit
}
},

moveit:function(e){
var evtobj=window.event? window.event : e
if (this.dragapproved==1){
this.targetobj.style.left=this.offsetx+evtobj.clie ntX-this.x+"px"
this.targetobj.style.top=this.offsety+evtobj.clien tY-this.y+"px"
return false

}
}
}

dragobject.initialize()

function getPosition(ev){
ev = ev || window.event;
var mousepos = getMouseCoords(ev)
if(mousepos.y < posTop()+50){
pageScrollUp(mousepos.y,ev)
}
}

function getMouseCoords(ev){
if(ev.pageX || ev.pageY){
return{x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}

function posTop(){
return typeof window.pageYOffset != 'undefined' ? windowPageYOffset :
document.documentElement
&& document.documentElement.scrollTop ?
document.documentElement.scrollTop : document.body.scrollTop ?
document.body.scrollTop : 0;
}

function pageScrollUp(mousePosY,ev){
window.scrollBy(0,-80);
var d = document.getElementById('myDiv');
ev = ev || window.event;
var mousepos = getMouseCoords(ev);

d.style.top = (mousePosY - 80)+'px';

}

</script>
</head><body>

<h1>Test test</h1><h1>Test test test</h1><h1>Test test test
test</h1><h1>Test test test test test</h1>
<h1>Test test test test test test</h1><h1>Test test test test
test</h1><h1>Test test test test</h1>
<h1>Test test test</h1><h1>Test test</h1><h1>Test test</h1><h1>Test
test test</h1><h1>Test test test test</h1>
<h1>Test test test test test</h1>

<div id="myDiv" class="drag" style="border:blue solid
1px;background-color:lightyellow;width:100px;"

onMouseMove='getPosition();'>Drag Me<br>Anywhere</div>

<h1>Test test</h1><h1>Test test test</h1><h1>Test test test
test</h1><h1>Test test test test test</h1>
<h1>Test test test test test test</h1><h1>Test test test test
test</h1><h1>Test test test test</h1>
<h1>Test test test</h1><h1>Test test</h1><h1>Test test</h1><h1>Test
test test</h1><h1>Test test test test</h1>
<h1>Test test test test test</h1>

</body></html>

Jan 18 '07 #1
1 3450
VK
vu******@gmail.com wrote:
Hello JavaScript experts,
I have a floating div which I drag all over the page.
Drag'n'drop is the rocket science if scripting :-) Really, to make a
really robust drag script ready for all circumstances and for all
UA-specific bugs requires much more than any AJAX or SOAP or anything.
This is why despite of myriads of "draggin-dropping" scripts written
over the last ten years there is a handful of really working in all
given environments. From free publically available I would suggest

<http://developer.yahoo.com/yui/dragdrop/>
samples at
<http://developer.yahoo.com/yui/examples/dragdrop/index.html>

This is if you are seeking a script for project. If you are learning
script over making a drag-n-drop script then someone else may help you
with that.

Jan 18 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: your name here | last post by:
I've been trying to get the scrollby javascript feature to scroll through frames. My end goal is to make my own scroll buttons to use for navigation through the frame but I figured I would start...
1
by: JF | last post by:
Hi, I need to change the location of the parent window and after that scroll a little bit (because of "position:fixed" css buttons, which obscure the title). I have...
1
by: BGC | last post by:
I'm attempting something like this: win1 = window.open("pop_up.html","_blank","width=325,height=272,top=0,left=0,scrollbars=0,resizable=0"); win1.window.scrollBy(100,50); I've done this...
3
by: Bubu | last post by:
Hello, excuse me bu I'm not a programmer. I have two frames, left navigation, right main In the left I have two buttons (up and down), I need to scroll the mainFrame up when I push the up button...
1
by: Michi | last post by:
Hallo. I just try to implement a scrolling effect to a drag and drop script. If you try to drag something to an area beneath the visible area of your browser window, the window should scroll...
3
by: lolo | last post by:
hello. happy new year. I'm trying to build a website for my wife and she is adament on having a horizontal thumbnail scrolling div. great. I have a good vertical scrolling thing, but can't...
3
by: vunet.us | last post by:
Hello, I have a floating div which I drag all over the page. I want to enable window.scrollBy function when div is dragged close to top or bottom of the page, so users would drag this div to the...
4
Eep˛
by: Eep˛ | last post by:
I'm trying to change the Firefox extension Scrollbar Anywhere so the mouse cursor does not move when scrolling. Here's the code but I don't know what to change: // Constants const sbawLeftButton ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.