h5学习--七个h5拖拽事件



  <script>
        // 七个h5拖拽事件
        const box = document.getElementById(‘box‘)
        const left = document.getElementById(‘left‘)
        const right = document.getElementById(‘right‘)
        let num = 0 

        //ondragstart  拖拽开始的时候,进行移动
        right.ondragstart= function(){
            console.log("lll")
            this.style.backgroundColor = "#ff0"
        }

        //ondrag 拖拽途中
        right.ondrag = function(){
            // console.log(num++)
        }

        //ondragend 拖拽结束松开鼠标时
        right.ondragend = function(){
            this.style.backgroundColor = "#eee"
        }

        //ondragenter 当有东西拖拽进来时
        left.ondragenter = function(){
            this.innerHTML = "释放鼠标"
            this.style.backgroundColor="#f00"
        }

        //ondragover 拖拽元素持续在目标身上摩擦移动
        left.ondragover = function(e){
            // console.log("目标拖拽元素持续在自己身上摩擦移动")
            // console.log(num++)
            e.preventDefault();
            e.stopPropagation();   //需要阻止时间冒泡和事件捕获
        }

        //ondragleave 当拖拽元素离开目标元素时
        left.ondragleave=function(){
            this.style.backgroundColor="#f60"
            this.innerHTML="拖拽到这里来"

            // document.body.box.removeChild(right)
        }

        // ondrop 拖拽元素丢尽目标元素身上
        left.ondrop = function(){
            this.innerHTML="拖拽到这里来"
            this.style.backgroundColor="#f60"
            console.log("拖拽元素丢尽目标元素身上")
            // document.body.removeChild(right);
            document.body.box.removeChild(right)

        }

    </script>

原文地址:https://www.cnblogs.com/mengtong/p/10888107.html

时间: 2024-08-10 23:44:46

h5学习--七个h5拖拽事件的相关文章

H5拖拽事件-- 自定义创建a标签热区

有两个BUG未更正, 1.拖拽后,动态创建的div始终在鼠标的下方,导致拖拽定位不准确 2.当窗口为自适应时,定位left为百分比,窗口改变大小时会存在定位偏差 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="./jquery-1

HTML5 02. 多媒体控件、拖拽事件、历史记录、web存储、应用程序缓存、地理定位、网络状态

多媒体 video:是行内块(text-align: center; 对行内块适用) <figure></figure>: 多媒体标签 : <figcaption></figcaption>: 多媒体标题 方法: load() 加载.play()播放.pause()暂停 属性: currentTime 视频播放的当前进度. duration:视频的总时间. paused:视频播放的状态 事件:   oncanplay:事件在用户可以开始播放视频/音频(aud

拖拽事件

<h1>拖拽事件</h1><ul>    <li>onmousedown:鼠标按下</li>    <li>onmousemove:鼠标移动</li>    <li>onmouseup:鼠标放开</li></ul>    <p>拖拽的时候如果有文字选中,会出现问题!: 当鼠标按下的时候,如果页面中有文字被选中,那么会触发浏览器的默认拖拽文字的效果</p>    &

Android Launcher拖拽事件详解【android4.0--Launcher系列二】

AndroidICS4.0版本的launcher拖 拽的流程,基本和2.3的相似.就是比2.3写的封装的接口多了一些,比如删除类的写法就多了个类.等等.4.0的改变有一些,但是不是特别大.这个月一 直在改动Launcher的缩略图的效果,4.0的缩略图的功能没有实现,还得从2.3的Launcher中摘出来.通过做这个缩略图对Launcher 的模块有一点点了解,拿来分享一下Launcher拖拽的工作流程.有图有真相!   (1) 先来看看类之间的继承关系      图(1)  (2)再来看看La

JS Event 鼠标拖拽事件

<!DOCTYPE html><html> <head>        <meta charset="UTF-8">        <title>JS Event鼠标拖拽事件</title>                <style>            #box{width:200px;height:200px;background:#000;position:absolute;}       

Duilib嵌入CEF禁止浏览器响应拖拽事件

在开发中有一个需求,拖拽外部文件到客户端,然后客户端响应WM_DROPFILES消息,在拖拽消息里处理一下业务,最后把处理结果显示到客户区html中,但实际中发现当拖拽文件到客户区,浏览器首先处理了拖拽事件,外层的Win32窗口无法捕捉到拖拽消息,因此要想实现此操作,刚开始我想了一个本办法,在客户区最外层罩了一个透明的Win32原生窗口,这样拖拽文件时,在最外层透明窗口的WM_DROPFILES消息中处理具体业务,最后用C++调用js函数,在页面显示处理结果. 方法一: 透明窗口 Layered

原生js实现 拖拽事件

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> .box { width: 200px; height: 200px; background-color: #000000; position: absolute; left: 0; top: 0;

pc端拖拽事件

<html><head> <meta charset="utf-8"/> <title></title> <style type="text/css"> #box{ width: 208px; height: 208px; background: url(girl.gif) no-repeat; position: absolute; } </style></head>&

拖拽事件_4

window.onload=function () { var oBox=document.getElementById("box"); var oUl=document.getElementById("oUl"); oBox.ondragenter=function () { this.innerHTML="可是释放鼠标了"; } oBox.ondragover=function(ev){ var ev=ev||event; ev.preven