- 浏览: 153565 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
驭乐MJ:
好!谢谢啦!正在学习使用sean中。。
Seam学习笔记 -
laorer:
00 -现在,互联网造就了一批富翁,但那时,似乎什么都不会去想 ...
如果时光能够回流到八年前 -
liuqizhi0925:
八年前,OMG ,能改变的事情真的很多...
如果时光能够回流到八年前
reference:http://www.webreference.com/programming/javascript/mk/column2/index.html
JavaScript excels at modifying the DOM of a Web page, but we usually only do simple things with it, such as creating image rollovers, making tabs, etc. This article is going to show you how to create items on your page that you can drag and drop.
There are several reasons you might want to encorporate this drag and drop ability into your Web pages. One of the simplest reasons is to reorganize Data. As an example, you might want to have a queue of items that your users can reorganize. Instead of putting an input or select box next to each item to represent its order, you could make the entire group of items draggable. Or perhaps you want to have a navigation window on your site that can be moved around by your users. Then there's always the simplest reason: because you can!
There's really not much involved with dragging an item around on your Web page. First we have to know where the mouse cursor is, second we need to know when the user clicks on an item so that we know we should be dragging it, and finally we need to move the item.
Demo - Drag any of the images
Demo - Drag and Drop any item
Capturing Mouse Movement
To start we need to capture the mouse coordinates. This is done by adding a function to document.onmousemove
:
1
document.onmousemove = mouseMove;
2
3
function mouseMove(ev){
4
ev = ev || window.event;
5
var mousePos = mouseCoords(ev);
6
}
7
8
function mouseCoords(ev){
9
if(ev.pageX || ev.pageY){
10
return {x:ev.pageX, y:ev.pageY};
11
}
12
return {
13
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
14
y:ev.clientY + document.body.scrollTop - document.body.clientTop
15
};
16
}
view plain | print | copy to clipboard | ?
We must first explain the event object. Whenever you move the mouse, click, press a key, etc., an event is fired. in Internet Explorer this event is global; it's stored in window.event. In Firefox, and other browsers, this event is passed to whatever function is attached to the action. Since we attached the mouseMove function to document.onmousemove
, mouseMove
gets passed the event object.
To make ev
contain the event object in every browser we OR
it with window.event
. In Firefox the " || window.event
" will be ignored since ev
already contains the event. In IE ev
is null so it will get set to window.event
.
Since we'll need to obtain the mouse coordinates many times over this article we make a mouseCoords
function that takes one argument: the event.
Again we run into differences between IE and other browsers. Firefox and other browsers use event.pageX
and event.pageY
to represent the mouse position relative to the document. If you have a 500x500 window and your mouse is in the middle, pageX
and pageY
will both be 250. If you then scroll down 500 pixels pageY will now by 750.
Contrary to this, IE decided to use event.clientX
and event.clientY
to represent the mouse position relative to the window, not the document. In our same example clientX
and clientY
will both be 250 if you put your mouse at the middle of a 500x500 window. If you scroll down on the page, clientY
will remain 250 since it is measured relative to the window and not where you are on the document. As a result we need to add the scrollLeft
and scrollTop
properties of the document body to our mouse position. Finally, the document in IE isn't actually at the 0,0 position. There is a small (usually 2px) border surrounding it. document.body.clientLeft
and clientTop
countain the width of this border, so we add those also to our mouse position.
Fortunately since we now have our mouseCoords
function we don't have to worry about this again.
Capturing Mouse Clicks
Next we need to know when your mouse button is pressed and when it is released. If we skip this step you would be "dragging" items whenever your mouse happened to move over them. This would be both annoying and counterintuitive.
There are two more functions that help us here: onmousedown
and onmouseup
. We previously attached a function to document.onmousemove
, so it only seems logical that we would attach functions to document.onmousedown
and document.onmouseup
. If we attach a function to document.onmousedown
, however, our function would get fired on any object we click on: text, images, tables, etc. We only want certain items on our page to be draggable so we instead attach a function to the onmousedown
event of whatever we want to move.
1
document.onmouseup = mouseUp;
2
var dragObject = null;
3
4
function makeClickable(object){
5
object.onmousedown = function(){
6
dragObject = this;
7
}
8
}
9
10
function mouseUp(ev){
11
dragObject = null;
12
}
view plain | print | copy to clipboard | ?
We now have a variable, dragObject
, that contains any item you click on. If you release the mouse button dragObject
gets set to null. So if dragObject != null
we know that we should be dragging something.
Demo - Click any image
You clicked on:
Moving an Item
We now know how to capture mouse movements and clicks. All that's left to do is move around whatever we want to drag. First, to move an item to exactly where we want it to be on a page, the style position of that item must be set to 'absolute'. Setting an item's position to absolute means that when you set a style.top
or style.left
on the item, the measurements are relative to the top-left of your page. Since all of our mouse movements are also relative to the top-left of our page, this is generally the way to go.
Once we set item.style.position='absolute'
, all we have to do is change the top or left position of the item and voila, it's moved!
1
document.onmousemove = mouseMove;
2
document.onmouseup = mouseUp;
3
4
var dragObject = null;
5
var mouseOffset = null;
6
7
function getMouseOffset(target, ev){
8
ev = ev || window.event;
9
10
var docPos = getPosition(target);
11
var mousePos = mouseCoords(ev);
12
return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
13
}
14
15
function getPosition(e){
16
var left = 0;
17
var top = 0;
18
19
while (e.offsetParent){
20
left += e.offsetLeft;
21
top += e.offsetTop;
22
e = e.offsetParent;
23
}
24
25
left += e.offsetLeft;
26
top += e.offsetTop;
27
28
return {x:left, y:top};
29
}
30
31
function mouseMove(ev){
32
ev = ev || window.event;
33
var mousePos = mouseCoords(ev);
34
35
if(dragObject){
36
dragObject.style.position = 'absolute';
37
dragObject.style.top = mousePos.y - mouseOffset.y;
38
dragObject.style.left = mousePos.x - mouseOffset.x;
39
40
return false;
41
}
42
}
43
function mouseUp(){
44
dragObject = null;
45
}
46
47
function makeDraggable(item){
48
if(!item) return;
49
item.onmousedown = function(ev){
50
dragObject = this;
51
mouseOffset = getMouseOffset(this, ev);
52
return false;
53
}
54
}
view plain | print | copy to clipboard | ?
发表评论
-
Nginx 常见应用技术指南
2006-08-26 00:00 744Nginx 常见应用技术指南 参考地址: http: ... -
apache2.2+tomcat5 配置笔记
2006-08-12 00:00 899apache2.2+tomcat5 配置笔记 ... -
最亲密接触Dhtml-JScript开发细节
2006-08-05 00:00 914转自:http://www.cnblogs.com/birds ... -
SSI(Server Side Include)
2006-07-29 00:00 1446使用SSI(Server Side Include)的 ... -
CSS学习笔记
2006-07-22 00:00 6381.一个良好的习惯是在命名类的时候,根据它们的功能而不 ... -
关于XPATH的文章
2006-07-15 00:00 984转自:http://www.matrix.org. ... -
http状态码
2006-07-01 00:00 738长整形标准http状态码,定义如下: Number ... -
XmlHttp中文参考
2006-06-24 00:00 809转自:http://www.xmlhttp.cn/ 最 ... -
JavaScript 随笔汇集[转]
2006-06-17 00:00 837最新的文章链接放在最上,并保持更新。 [SCRIP ... -
window.showModalDialog以及window.open用法简介
2006-06-10 00:00 1082window.showModalDialog以及window. ... -
jQuery与prototype的比较
2006-06-03 00:00 999jQuery与prototype的比较 ... -
推荐几款JavaScript日历选择器
2006-05-27 00:00 29041. Site:http://www.dynarch. ... -
JavaScript学习笔记 2
2006-05-13 00:00 9241. 如果你正访问的网站在本地存有cookie,那 ... -
JavaScript学习笔记
2006-05-06 00:00 960JavaScript学习笔记 ... -
一次登录,资源尽享(Single Sing-On)
2006-04-29 00:00 1278Single Sing-On简介 微软已经推 ... -
Apache学习笔记
2006-04-15 00:00 1018Apache HTTP Server Version 2.2 ... -
PHP学习笔记
2006-04-01 00:00 1234* 学习网址: PHP中文手册 http: ...
相关推荐
- Understanding how to use these APIs to create more dynamic and interactive web applications. - Implementing these features to enhance user experience and functionality. 6. **Becoming Skilled in ...
In this tutorial, we'll cover some of the basics of Unicode-encoded text and Unicode files, and how to view and manipulate it in UltraEdit. Search and delete lines found UEStudio and UltraEdit provide...
Use the new HTML5 JavaScript APIs: canvas, messaging, drag and drop, workers and sockets, etc. Learn about related JavaScript APIs including touch events, device orientation, and WebGL. Who this book ...
Work with drag-and-drop in HTML5 Discover how geolocation works in different browsers Master HTML5 canvas theory with lots of code samples Understand how offline web application works and learn about ...
You will learn how to use various APIs for asynchronous calls, GeoLocation, Audio, File Drag and Drop, touch events, and more. Later on, you will build custom elements with Web Components and build ...
This training guide is designed for information technology (IT) professionals who ...■■ Perform drag and drop operations. ■■ Make your HTML location aware. ■■ Persist data on the browser client.
在描述中提到的"obsolete-datagrid-how-to-reorder-rows-using-drag-and-drop-t152596"是一个关于如何在DataGrid中使用拖放功能对行进行重新排序的示例。在早期版本的DevExtreme中,开发者可能需要自定义事件处理...
Drag-and-drop Part 3: Building an application Chapter 13. Class system foundations Chapter 14. Building an application Book Details Title: Ext JS in Action, 2nd Edition Author: Grgur Grisogono, ...
To include this data, we need to wrap the BAPI call in a custom function module and modify the result table to include the lock status information of users. ### Custom Function Module Create a new ...
Just drag the LoginControl icon in the ToolBox pane and drop it to the Web form. The control should appear in the form with all its child controls. By manipulating the Property window, you can design...
Accessibility Drag n Drop Input Methods Image I/O Print Service Sound Java SE API Integration Libraries IDL JDBC JNDI RMI RMI-IIOP Scripting Other Base Libraries Beans Int'l Support Input/Output...
Sortable is a <s>minimalist</s> JavaScript library for reorderable drag-and-drop lists. Demo: http://rubaxa.github.io/Sortable/ ## Features * Supports touch devices and [modern]...
This practical guide shows you how to make your Java web applications more responsive and dynamic by incorporating new Ajaxian features, including suggestion lists, drag-and-drop, and more. Java ...
This practical guide shows you how to make your Java web applications more responsive and dynamic by incorporating new Ajaxian features, including suggestion lists, drag-and-drop, and more. Java ...
This practical guide shows you how to make your Java web applications more responsive and dynamic by incorporating new Ajaxian features, including suggestion lists, drag-and-drop, and more. Java ...
Easy drag and drop familiar interface. Resize, change dimensions, scale, crop, add text, optimize, rotate, flip, mirror and add watermark. 控制上传文件夹大小 <br/>Max Upload Folder ...
对于拖放操作,可以使用Actions类来实现,如`new Actions(driver).dragAndDrop(source, target).perform();`。 **3.2.12 导航 (Navigation and History)** 导航涉及到前进、后退、刷新等操作,可以通过`navigate()....