一、阻止默认行为

  return false;

自定义右键菜单

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
*{ margin:0; padding:0;}
#ul1{ list-style:none; display:none; width:100px; padding:5px; background:#ddd; border:1px solid ddd; color:#333; line-height:24px; position:absolute;}
</style>
<script>
document.oncontextmenu = function(ev)
{
    var oEvent = ev || event;
    var oUl = document.getElementById('ul1');
    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;

    oUl.style.left = oEvent.clientX + 'px';
    oUl.style.top = oEvent.clientY + scrollTop + 'px';
    oUl.style.display = 'block';
    return false;
};

document.onclick = function()
{
    var oUl = document.getElementById('ul1');
    oUl.style.display = 'none';
};

</script>
</head>

<body style="height:2000px;">
<ul id="ul1">
    <li>复制</li>
    <li>粘贴</li>
    <li>剪切</li>
    <li>撤销</li>
</ul>
</body>
</html>

拖拽

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1{ width:100px; height:100px; background:red; position:absolute;}
</style>
<script>
window.onload = function()
{
    var oDiv = document.getElementById('div1');
    var disX = 0;
    var disY = 0;

    oDiv.onmousedown = function(ev)
    {
        var oEvent = ev || event;
        disX = oEvent.clientX - oDiv.offsetLeft;
        disY = oEvent.clientY - oDiv.offsetTop;

        document.onmousemove = function(ev)
        {
            var oEvent = ev || event;
            var iX = oEvent.clientX - disX;
            var iY = oEvent.clientY - disY;

            if(iX < 0)
            {
                iX = 0;
            }
            else if( iX > document.documentElement.clientWidth - oDiv.offsetWidth)
            {
                iX = document.documentElement.clientWidth - oDiv.offsetWidth;
            }
            if(iY < 0)
            {
                iY = 0;
            }else if( iY > document.documentElement.clientHeight - oDiv.offsetHeight)
            {
                iY = document.documentElement.clientHeight - oDiv.offsetHeight;
            }

            oDiv.style.left = iX + 'px';
            oDiv.style.top = iY + 'px';
        };

        document.onmouseup = function()
        {
            document.onmousemove = null;
            document.onmouseup = null;
        };

        return false;

    };
};
</script>
</head>

<body>
<div id="div1"></div>
</body>
</html>

javascript每日一练(八)——事件三:默认行为的更多相关文章

  1. javascript每日一练(七)——事件二:键盘事件

    一.键盘事件 onkeydown触发, keyCode键盘编码 ctrlKey altKey shiftKey 键盘控制div移动 <!doctype html> <html> ...

  2. javascript每日一练(六)——事件一

    一.event对象 var oEvent = ev || event;//获取事件对象 oEvent.clientX oEvent.clientY//获取鼠标坐标 oEvent.cancelBubbl ...

  3. javascript每日一练(三)——DOM一

    一.Dom基础 childNodes(有兼容问题),children nodeType getAttribute() firstChild,lastChild,previousSilbing,next ...

  4. javascript每日一练(一)——javascript基础

    一.javascript的组成 ECMAScript DOM BOM 二.变量类型 常见类型有:number, string, boolean, undefined, object, function ...

  5. javascript每日一练(十三)——运动实例

    一.图片放大缩小 <!doctype html> <html> <head> <meta charset="utf-8"> < ...

  6. javascript每日一练(五)——BOM

    一.BOM打开,关闭窗口 window.open(); window.close(); <!doctype html> <html> <head> <meta ...

  7. javascript每日一练—运动

    1.弹性运动 运动原理:加速运动+减速运动+摩擦运动: <!doctype html> <html> <head> <meta charset="u ...

  8. javascript每日一练(十四)——弹性运动

    一.弹性运动 运动原理:加速运动+减速运动+摩擦运动: <!doctype html> <html> <head> <meta charset="u ...

  9. javascript每日一练(十二)——运动框架

    运动框架 可以实现多物体任意值运动 例子: <!doctype html> <html> <head> <meta charset="utf-8&q ...

随机推荐

  1. cocos2d-x 源代码 :可以循环CCScrollView (,代码已被重构连接使用)

    cocos2d-x来源合计文件夹 http://blog.csdn.net/u011225840/article/details/31743129 1.准备工作 想弄懂可循环的CCscrollView ...

  2. 循环训练(for的嵌套、while、do while)以及异常处理

    For的嵌套 练习一: 练习二: 练习三: 练习四: while的使用方法: 示例一: 示例二: 示例三: while的练习题: do while的使用示例: 异常处理示例: try   catch  ...

  3. Longest Palindromic Substring - 一题多解

    题意是寻找一个字符串的最大回文字串,最简单的是n3方的算法,由于字符串最大长度为1000,所以这个方法很危险而且不科学. 紧接着想到的是一个n方的算法:回文子串是从中间向两边产生的,那么对于每个字符考 ...

  4. Jmeter 发送json{Jfinal 接口req rsp均为json}

    链接地址:http://yangyoupeng-cn-fujitsu-com.iteye.com/blog/2013649 使用jmeter发送json数据.方法有三种 原创,转载请注明出处 1.利用 ...

  5. JavaSE学习总结第06天_Java语言基础2 & 面向对象1

      06.01 二维数组概述和格式1的讲解 二维数组概述:二维数组其实就是一个元素为一维数组的数组 格式1:数据类型[][] 变量名 = new 数据类型[m][n]; m表示这个二维数组有多少个一维 ...

  6. Oracle数据库的启动和关闭实例

    在开始了解oracle数据库的命令之前,先来看一个东西:SQL*PLUS(sqlplus) Oracle的sql*plus是与oracle进行交互的客户端工具.在sql*plus中,可以运行sql*p ...

  7. beta分布

    http://blog.csdn.net/sweetrryy/article/details/6436358

  8. ASP.NET jQuery 随笔 显示CheckBoxList成员选中的内容

    通过jQuery来获取CheckBoxList成员内容. <%@ Page Language="C#" AutoEventWireup="true" Co ...

  9. perl学习(2) 基本数据类型等

    1.1.数字 所有数字格式内部一致,全部是double 7.25e45   == 7.25 * 1045 5.25 6.00 5.1-2.4          #5.1-2.4,2.7 10/3    ...

  10. 将HDC保存为BMP文件

    HDC在MSDN中的全称为:The handle of device context.通常,我们都是用来做相应的显示操作.        熟悉WIN32的朋友对于其应该不会陌生,经常采用GetDC,G ...