阻止右键菜单(阻止默认事件)&&跟随鼠标移动(大图展示)&&自定义右键菜单

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>阻止右键菜单(阻止默认事件)</title>
<style>
pre{color:green;padding:10px 15px;background:#f0f0f0;border:1px dotted #333;font:12px/1.5 Courier New;}
span{color:#999;}
</style>
<script type="text/javascript">
document.oncontextmenu = function ()
{
    return false
}
</script>
</head>
<body>
<pre>
&lt;script type="text/javascript"&gt;
<span>//※oncontextmenu</span>
document.oncontextmenu = function ()
{
    return false
}
&lt;/script&gt;
</pre>
</body>
</html>
跟随鼠标移动(大图展示)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>跟随鼠标移动(大图展示)</title>
<style type="text/css">
html,body{overflow:hidden;}
body,div,ul,li{margin:0;padding:0;}
#box ul{width:768px;height:172px;list-style-type:none;margin:10px auto;}
#box li{float:left;width:170px;height:170px;cursor:pointer;display:inline;border:1px solid #ddd;margin:0 10px;}
#box li.active{border:1px solid #a10000;}
#box li img{width:170px;height:170px;vertical-align:top;}
#big{position:absolute;width:400px;height:400px;border:2px solid #ddd;display:none;}
#big div{position:absolute;top:0;left:0;width:400px;height:400px;opacity:0.5;filter:alpha(opacity=50);background:#fff url(img/loading.gif) 50% 50% no-repeat;}
</style>
<script type="text/javascript">
window.onload = function ()
{
    var aLi = document.getElementsByTagName("li");
    var oBig = document.getElementById("big");
    var oLoading = oBig.getElementsByTagName("div")[0];
    var i = 0;
    
    for (i = 0; i < aLi.length; i++)
    {
        aLi[i].index = i;
        //鼠标划过, 预加载图片插入容器并显示
        aLi[i].onmouseover = function ()
        {
            var oImg = document.createElement("img");
            //图片预加载
            var img = new Image();            
            img.src = oImg.src = aLi[this.index].getElementsByTagName("img")[0].src.replace(".jpg","_big.jpg");
            //插入大图片
            oBig.appendChild(oImg);
            //鼠标移过样式
            this.className = "active";
            //显示big
            oBig.style.display = oLoading.style.display = "block";
            //判断大图是否加载成功
            img.complete ? oLoading.style.display = "none" : (oImg.onload = function() {oLoading.style.display = "none";})        
        };
        //鼠标移动, 大图容器跟随鼠标移动
        aLi[i].onmousemove = function (event)
        {
            var event = event || window.event;    
            var iWidth = document.documentElement.offsetWidth - event.clientX;    
            //设置big的top值
            oBig.style.top = event.clientY + 20 + "px";
            //设置big的left值, 如果右侧显示区域不够, 大图将在鼠标左侧显示.
            oBig.style.left = (iWidth < oBig.offsetWidth + 10 ? event.clientX - oBig.offsetWidth - 10 : event.clientX + 10) + "px";
            
        };
        //鼠标离开, 删除大图并隐藏大图容器
        aLi[i].onmouseout = function ()
        {
            this.className = "";
            oBig.style.display = "none";
            //移除大图片
            oBig.removeChild(oBig.lastChild)
        }
    }
};
</script>
</head>
<body>
<div id="box">
    <ul>
        <li><img src="img/shirt_1.jpg" /></li>
        <li><img src="img/shirt_2.jpg" /></li>
        <li><img src="img/shirt_3.jpg" /></li>
        <li><img src="img/shirt_4.jpg" /></li>
    </ul>
</div>
<div id="big"><div></div></div>
</body>
</html>
自定义右键菜单
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>自定义右键菜单</title>
<style type="text/css">
body,ul,li{margin:0;padding:0;}
body{font:12px/24px arial;}
#menu{position:absolute;top:-9999px;left:-9999px;width:100px;border-radius:3px;list-style-type:none;border:1px solid #8f8f8f;padding:2px;background:#fff;}
#menu li{position:relative;height:24px;padding-left:24px;background:#eaead7;vertical-align:top;}
#menu li a{display:block;color:#333;background:#fff;padding-left:5px;text-decoration:none;}
#menu li.active{background:#999;}
#menu li.active a{color:#fff;background:#8f8f8f;}
#menu li em{position:absolute;top:0;left:0;width:24px;height:24px;background:url(img/ico.png) no-repeat;}
#menu li em.cur{background-position:0 0;}
#menu li em.copy{background-position:0 -24px;}
#menu li em.paste{background-position:0 -48px;}
</style>
<script type="text/javascript">
window.onload = function ()
{
    var oMenu = document.getElementById("menu");
    var aLi = oMenu.getElementsByTagName("li");
    //加载后隐藏自定义右键菜单
    oMenu.style.display = "none";
    //菜单鼠标移入/移出样式
    for (i = 0; i < aLi.length; i++)
    {
        //鼠标移入样式
        aLi[i].onmouseover = function ()
        {
            this.className = "active"    
        };
        //鼠标移出样式
        aLi[i].onmouseout = function ()
        {
            this.className = ""    
        }
    }
    //自定义菜单
    document.oncontextmenu = function (event)
    {
        var event = event || window.event;
        var style = oMenu.style;
        style.display = "block";
        style.top = event.clientY + "px";
        style.left = event.clientX + "px";
        return false;
    };
    //页面点击后自定义菜单消失
    document.onclick = function ()
    {
        oMenu.style.display = "none"    
    }
};
</script>
</head>
<body>
<center>自定义右键菜单,请在页面点击右键查看效果。</center>
<ul id="menu">
    <li><em class="cut"></em><a href="javascript:;">剪切</a></li>
    <li><em class="copy"></em><a href="javascript:;">复制</a></li>
    <li><em class="paste"></em><a href="javascript:;">粘贴</a></li>
</ul>
</body>
</html>

 

posted @ 2015-05-03 23:22  mayufo  阅读(366)  评论(0编辑  收藏  举报