Home Web Front-end JS Tutorial Google-like Div drag effect code implemented in Javascript_javascript skills

Google-like Div drag effect code implemented in Javascript_javascript skills

May 16, 2016 pm 06:03 PM
google

Copy code The code is as follows:

JScript file:
//Check the browser MSIE Firefox
var ie=false,moz=false;
(function()
{//check the browser
var userAgent=navigator.userAgent;
if(userAgent.indexOf("MSIE")!=-1)
ie=true;
else if(userAgent.indexOf("Firefox")!=-1 )
moz=true;
})();
//Get the object by ID
function $E_ID(idString)
{
return document.getElementById(idString);
}
//Get the object by Name
function $Es_Tag(tagName)
{
return document.getElementsByTagName(tagName);
}
//Get the absolute position of the object obj .offsetparent
function $GetInfo(o)
{
var to=new Object();
to.left=to.right=to.top=to.bottom=0;
var twidth=o.offsetWidth;
var theight=o.offsetHeight;
while(o)
{
to.left =o.offsetLeft;
to.top =o.offsetTop;
o=o.offsetParent;
}
to.right=to.left twidth;
to.bottom=to.top theight;
return to;
}
//The event is confirmed when the mouse clicks on the object
function $hitTest(obj,event)
{
obj=$GetInfo(obj);
var x=event.clientX;
var y= event.clientY;
if((x>=obj.left&&x<=obj.right)&&(y>=obj.top&&y<=obj.bottom))
return true;
else
return false;
}
function Drag(event)
{
this.dragged=false;
this.ao=null;
this.tdiv=null;
this .fixLeft=0;
this.fixTop=0;
this.lastX=event.clientX;
this.lastY=event.clientY;
Drag.mm=null;
this. dragStart=function(event)
{
this.ao=ie?event.srcElement:(moz?event.target:null);
if(ie)
document.body.onselectstart=function ()
{
return false
 }
if(moz&&this.ao.nodeType==3)
this.ao=this.ao.parentNode;
if(this.ao .tagName=="TD"||this.ao.tagName=="TR")
this.ao=this.ao.offsetParent.parentNode;
else
return;
if(this .ao.className!="dragdiv")
return;
this.tdiv=$E_ID("tmpdiv");
this.tdiv.style.visibility="visible";
this. tdiv.style.filter="alpha(opacity=70)";
if(ie)
 this.tdiv.filters.alpha.opacity=70;
this.tdiv.style.opacity=0.7;
this.tdiv.style.zIndex=100;
this.tdiv.innerHTML=this.ao.innerHTML;
this.tdiv.style.width=this.ao.offsetWidth "px";
this.tdiv.style.height=this.ao.offsetHeight "px";
this.tdiv.style.top=$GetInfo(this.ao).top "px";
this.tdiv.style .left=$GetInfo(this.ao).left "px";
this.fixTop=parseInt($GetInfo(this.tdiv).top);
this.fixLeft=parseInt($GetInfo(this. tdiv).left);
this.dragged=true;
}
this.onDrag=function(event)
{
if((!this.dragged)||this.ao ==null)return;
var tX=event.clientX;
var tY=event.clientY;
this.tdiv.style.left=parseInt(this.fixLeft tX-this.lastX-document. body.scrollLeft) "px";
this.tdiv.style.top=parseInt(this.fixTop tY-this.lastY-document.body.scrollTop) "px";
for(var m=0; m<$E_ID("root").rows.length;m )
{
var rootCells=$E_ID("root").rows[m].cells;
for(var i=0 ;i{
if($hitTest(rootCells[i],event))
{
if(rootCells[i].hasChildNodes())
{
for(var j=0;j{
if($hitTest(rootCells[i].childNodes[j],event))
{
rootCells[i].insertBefore(this.ao,rootCells[i].childNodes[j]);
break;
}
}
if(j==rootCells [i].childNodes.length)
{
rootCells[i].appendChild(this.ao);break;
}
break;
}
else
{
rootCells[i].appendChild(this.ao);
break;
}
}
}
}
}
this.dragEnd=function()
{
if(this.dragged)
{
this.dragged=false;
Drag.mm=this.repos(150,15,this);
this.ao =null;
}
if(ie)document.body.onselectstart=function(){return true}
}
this.repos=function(aa,ab,obj)
{
if(ie)var f=obj.tdiv.filters.alpha.opacity;
else if(moz)var f=obj.tdiv.style.opacity*100;
var kf=f/ab ;
var tl=parseInt($GetInfo(obj.tdiv).left);
var tt=parseInt($GetInfo(obj.tdiv).top);
var kl=(tl-$GetInfo (obj.ao).left)/ab;
var kt=(tt-$GetInfo(obj.ao).top)/ab;
return setInterval(function(){
if(ab< 1)
{
clearInterval(Drag.mm);
obj.tdiv.style.visibility="hidden";
obj.tdiv.style.zIndex="";
return;
}
ab--;
tl-=kl;
tt-=kt;
f-=kf;
obj.tdiv.style.left=parseInt(tl) "px";
obj.tdiv.style.top=parseInt(tt) "px";
if(ie)obj.tdiv.filters.alpha.opacity=f;
else if(moz) obj.tdiv.style.opacity=f/100;
},aa/ab );
}
}
var tDrag=null;
function init(event)
{
// alert(event.target.innerHTML);
tDrag=new Drag(event);
tDrag.dragStart(event);
}
function move(event)
{
if(tDrag!=null)tDrag.onDrag(event);
}
function end()
{
if(tDrag!=null){
tDrag.dragEnd ();
tDrag=null;
}
}
Asp.net文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>



Div拖动





























可移动DIV1

点击即可开始拖动!









可移动DIV2

点击即可开始拖动!









可移动DIV3

点击即可开始拖动!











Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Google Pixel 9 and Pixel 9 Pro rumoured to gain Creative Assistant AI upon release Google Pixel 9 and Pixel 9 Pro rumoured to gain Creative Assistant AI upon release Jun 22, 2024 am 10:50 AM

Currently, four new Pixel smartphones are anticipated to land this autumn. To recap, the series is rumoured to feature thePixel 9 and Pixel 9 Pro at launch. However, the Pixel 9 Pro will be a rival to the iPhone 16 Pro rather than a Pixel 8 Pro (curr

Google Pixel 9 Pro XL gets tested with desktop mode Google Pixel 9 Pro XL gets tested with desktop mode Aug 29, 2024 pm 01:09 PM

Google has introduced DisplayPort Alternate Mode with the Pixel 8 series, and it's present on the newly launched Pixel 9 lineup. While it's mainly there to let you mirror the smartphone display with a connected screen, you can also use it for desktop

Google Tensor G4 of Pixel 9 Pro XL lags behind Tensor G2 in Genshin Impact Google Tensor G4 of Pixel 9 Pro XL lags behind Tensor G2 in Genshin Impact Aug 24, 2024 am 06:43 AM

Google recently responded to the performance concerns about the Tensor G4 of the Pixel 9 line. The company said that the SoC wasn't designed to beat benchmarks. Instead, the team focused on making it perform well in the areas where Google wants the c

Google app beta APK teardown reveals new extensions coming to Gemini AI assistant Google app beta APK teardown reveals new extensions coming to Gemini AI assistant Jul 30, 2024 pm 01:06 PM

Google's AI assistant, Gemini, is set to become even more capable, if the APK teardown of the latest update (v15.29.34.29 beta) is to be considered. The tech behemoth's new AI assistant could reportedly get several new extensions. These extensions wi

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI has started to provide developers with access to extended context windows and cost-saving features, starting with the Gemini 1.5 Pro large language model (LLM). Previously available through a waitlist, the full 2 million token context windo

Google Pixel 9 smartphones will not launch with Android 15 despite seven-year update commitment Google Pixel 9 smartphones will not launch with Android 15 despite seven-year update commitment Aug 01, 2024 pm 02:56 PM

The Pixel 9 series is almost here, having been scheduled for an August 13 release. Based on recent rumours, the Pixel 9, Pixel 9 Pro and Pixel 9 Pro XL will mirror the Pixel 8 and Pixel 8 Pro (curr. $749 on Amazon) by starting with 128 GB of storage.

New Google Pixel desktop mode showcased in fresh video as possible Motorola Ready For and Samsung DeX alternative New Google Pixel desktop mode showcased in fresh video as possible Motorola Ready For and Samsung DeX alternative Aug 08, 2024 pm 03:05 PM

A few months have passed since Android Authority demonstrated a new Android desktop mode that Google had hidden away within Android 14 QPR3 Beta 2.1. Arriving hot on the heels of Google adding DisplayPort Alt Mode support for the Pixel 8 and Pixel 8

Leaked Google Pixel 9 adverts show new AI features including \'Add Me\' camera functionality Leaked Google Pixel 9 adverts show new AI features including \'Add Me\' camera functionality Jul 30, 2024 am 11:18 AM

More promotional materials relating to the Pixel 9 series have leaked online. For reference, the new leak arrived shortly after 91mobiles shared multiple images that also showcased the Pixel Buds Pro 2 and Pixel Watch 3 or Pixel Watch 3 XL. This time

See all articles