JavaScript drag and drop effect_html/css_WEB-ITnose
Jun 24, 2016 am 11:53 AM
Drag and drop effect, also called drag and drop. A simple drag and drop effect
Drag a div
[Program description]
First initialize the function, for drag and drop The object adds a mousedown listening event
initialize:function(drag){ this.Drag = $$(drag);//拖放對(duì)象 this._x = this._y = 0;//記錄鼠標(biāo)相對(duì)于拖放對(duì)象的位置 this._fM = BindAsEventListener(this, this.Move); //綁定拖動(dòng)時(shí)執(zhí)行的函數(shù) this._fS = Bind(this, this.Stop); //綁定拖動(dòng)結(jié)束執(zhí)行的函數(shù) this.Drag.style.position = "absolute"; EventUtil.addEventHandler(this.Drag, "mousedown", BindAsEventListener(this, this.Start)); //監(jiān)聽(tīng)拖動(dòng)對(duì)象mousedown事件 }
and then adds mousemove and mouseup events in the Start method to monitor the position of the drag and drop object and cancel the listening event respectively
Start:function(oEvent){ //記錄mousedown觸發(fā)時(shí)鼠標(biāo)相對(duì)于拖放對(duì)象的位置 this._x = oEvent.clientX - this.Drag.offsetLeft; this._y = oEvent.clientY - this.Drag.offsetTop; //監(jiān)聽(tīng)mousemove 和 mouseup事件 EventUtil.addEventHandler(document, "mousemove", this._fM); EventUtil.addEventHandler(document, "mouseup", this._fS); }
Full drag and drop program:
/** *拖放程序 */ var simpleDrag = Class.create(); simpleDrag.prototype = { //初始化對(duì)象 initialize:function(drag){ this.Drag = $$(drag);//拖放對(duì)象 this._x = this._y = 0;//記錄鼠標(biāo)相對(duì)于拖放對(duì)象的位置 this._fM = BindAsEventListener(this, this.Move); //綁定拖動(dòng)時(shí)執(zhí)行的函數(shù) this._fS = Bind(this, this.Stop); //綁定拖動(dòng)結(jié)束執(zhí)行的函數(shù) this.Drag.style.position = "absolute"; EventUtil.addEventHandler(this.Drag, "mousedown", BindAsEventListener(this, this.Start)); //監(jiān)聽(tīng)拖動(dòng)對(duì)象mousedown事件 }, //準(zhǔn)備拖動(dòng) Start:function(oEvent){ //記錄mousedown觸發(fā)時(shí)鼠標(biāo)相對(duì)于拖放對(duì)象的位置 this._x = oEvent.clientX - this.Drag.offsetLeft; this._y = oEvent.clientY - this.Drag.offsetTop; //監(jiān)聽(tīng)mousemove 和 mouseup事件 EventUtil.addEventHandler(document, "mousemove", this._fM); EventUtil.addEventHandler(document, "mouseup", this._fS); }, //拖動(dòng) Move:function(oEvent){ this.Drag.style.left = oEvent.clientX - this._x + "px"; this.Drag.style.top = oEvent.clientY - this._y + "px"; }, //停止拖動(dòng) Stop:function(){ EventUtil.removeEventHandler(document, "mousemove", this._fM); EventUtil.removeEventHandler(document, "mouseup", this._fS); }, }; //初始化拖動(dòng) new simpleDrag('drag');
Full DEMO:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>JavaScript拖放效果</title> <style type="text/css"> *{margin:0;padding:0;} .drag-wrap{height: 300px;margin:10px;border:5px solid #FF8000;background:#8080C0;position: relative;} .draggable{width: 100px;height: 100px;position: absolute;top: 100px;left:100px;background:#eee;border:1px solid #aceaac;cursor: move;} </style></head><body> <div class="drag-wrap"> <div class="draggable" id="drag">拖動(dòng)div</div> </div> <div class="drag-status" id="drag-status" style="height:30px;padding:0 10px;"></div> <script type="text/javascript"> /** *工具函數(shù) */ var isIE = (document.all) ? true : false ; var $$ = function(id){ return "string" == typeof id ? document.getElementById(id) : id; }; var Class = { create: function() { return function() { this.initialize.apply(this, arguments); } } }; var Extend = function(destination,source){ for(var property in source){ destination[property] = source[property]; } }; var BindAsEventListener = function(object,func){ return function(event){ return func.call(object,event || window.event); } }; var Bind = function(object,func){ return function(){ return func.apply(object,arguments); } }; /** *跨瀏覽器事件對(duì)象 */ var EventUtil = { //事件注冊(cè)處理程序 addEventHandler:function(oTarget,sEventType,fnHandler){ if(oTarget.addEventListener){ oTarget.addEventListener(sEventType,fnHandler,false); }else if(oTarget.attachEvent){ oTarget.attachEvent("on"+sEventType,fnHandler); }else{ oTarget["on"+sEventType] = fnHandler; } }, //事件移除處理程序 removeEventHandler:function(oTarget,sEventType,fnHandler){ if(oTarget.removeEventListener){ oTarget.removeEventListener(sEventType,fnHandler,false); }else if(oTarget.detachEvent){ oTarget.detachEvent("on"+sEventType,fnHandler); }else{ oTarget["on"+sEventType] = null; } }, getEvent:function(event){ return event ? event : window.event ; }, getTarget:function(event){ return event.target || event.srcElement; } }; /** *拖放程序 */ var simpleDrag = Class.create(); simpleDrag.prototype = { //初始化對(duì)象 initialize:function(drag){ this.Drag = $$(drag);//拖放對(duì)象 this._x = this._y = 0;//記錄鼠標(biāo)相對(duì)于拖放對(duì)象的位置 this._fM = BindAsEventListener(this, this.Move); //綁定拖動(dòng)時(shí)執(zhí)行的函數(shù) this._fS = Bind(this, this.Stop); //綁定拖動(dòng)結(jié)束執(zhí)行的函數(shù) this.Drag.style.position = "absolute"; EventUtil.addEventHandler(this.Drag, "mousedown", BindAsEventListener(this, this.Start)); //監(jiān)聽(tīng)拖動(dòng)對(duì)象mousedown事件 }, //準(zhǔn)備拖動(dòng) Start:function(oEvent){ //記錄mousedown觸發(fā)時(shí)鼠標(biāo)相對(duì)于拖放對(duì)象的位置 this._x = oEvent.clientX - this.Drag.offsetLeft; this._y = oEvent.clientY - this.Drag.offsetTop; //監(jiān)聽(tīng)mousemove 和 mouseup事件 EventUtil.addEventHandler(document, "mousemove", this._fM); EventUtil.addEventHandler(document, "mouseup", this._fS); }, //拖動(dòng) Move:function(oEvent){ this.Drag.style.left = oEvent.clientX - this._x + "px"; this.Drag.style.top = oEvent.clientY - this._y + "px"; }, //停止拖動(dòng) Stop:function(){ EventUtil.removeEventHandler(document, "mousemove", this._fM); EventUtil.removeEventHandler(document, "mouseup", this._fS); }, }; //初始化拖動(dòng) new simpleDrag('drag'); </script></body></html>

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

ARIA's role attribute is used to define the role of web elements and improve accessibility. 1. Role attribute helps assistive technology to understand the functions of elements, such as buttons, navigation, etc. 2. Use role attributes to assign specific roles to non-semantic HTML elements. 3. The role attribute should be consistent with the element behavior and be verified by the accessibility tool test.

How to create a website layout? 1. Use HTML tags to define the content structure, such as, ,. 2. Control styles and positions through CSS, using box model, float or Flexbox layout. 3. Optimize performance, reduce HTTP requests, use cache and optimize images, and ensure responsive design.

Improve the readability and maintainability of HTML code can be achieved through the following steps: 1. Use semantic tags, such as, etc. to make the code structure clear and improve SEO effect; 2. Keep the code formatted and use consistent indentation and spaces; 3. Add appropriate comments to explain the code intention; 4. Avoid excessive nesting and simplify the structure; 5. Use external style sheets and scripts to keep the HTML concise.

The key to keep up with HTML standards and best practices is to do it intentionally rather than follow it blindly. First, follow the summary or update logs of official sources such as WHATWG and W3C, understand new tags (such as) and attributes, and use them as references to solve difficult problems; second, subscribe to trusted web development newsletters and blogs, spend 10-15 minutes a week to browse updates, focus on actual use cases rather than just collecting articles; second, use developer tools and linters such as HTMLHint to optimize the code structure through instant feedback; finally, interact with the developer community, share experiences and learn other people's practical skills, so as to continuously improve HTML skills.

The reason for using tags is to improve the semantic structure and accessibility of web pages, make it easier for screen readers and search engines to understand page content, and allow users to quickly jump to core content. Here are the key points: 1. Each page should contain only one element; 2. It should not include content that is repeated across pages (such as sidebars or footers); 3. It can be used in conjunction with ARIA properties to enhance accessibility. Usually located after and before, it is used to wrap unique page content, such as articles, forms or product details, and should be avoided in, or in; to improve accessibility, aria-labeledby or aria-label can be used to clearly identify parts.

To create a basic HTML document, you first need to understand its basic structure and write code in a standard format. 1. Use the declaration document type at the beginning; 2. Use the tag to wrap the entire content; 3. Include and two main parts in it, which are used to store metadata such as titles, style sheet links, etc., and include user-visible content such as titles, paragraphs, pictures and links; 4. Save the file in .html format and open the viewing effect in the browser; 5. Then you can gradually add more elements to enrich the page content. Follow these steps to quickly build a basic web page.

To create an HTML checkbox, use the type attribute to set the element of the checkbox. 1. The basic structure includes id, name and label tags to ensure that clicking text can switch options; 2. Multiple related check boxes should use the same name but different values, and wrap them with fieldset to improve accessibility; 3. Hide native controls when customizing styles and use CSS to design alternative elements while maintaining the complete functions; 4. Ensure availability, pair labels, support keyboard navigation, and avoid relying on only visual prompts. The above steps can help developers correctly implement checkbox components that have both functional and aesthetics.

HTMLtagsareessentialforstructuringwebpages.Theydefinecontentandlayoutusinganglebrackets,ofteninpairslikeand,withsomebeingself-closinglike.HTMLtagsarecrucialforcreatingstructured,accessible,andSEO-friendlywebpages.
