[整理]网页自定义鼠标右键菜单特效

演示: http://www.yes1000.com/demo/menu/

JS文件:
程序代码
// 菜单的HTML代码
var strMenu = "<div id=/"menu/" class=/"clsMenu/" onMouseover=/"highlight()/" onMouseout=/"lowlight()/">"
strMenu += "<div class=/"menuitems/" onClick=/"this.style.behavior='url(#default#homepage)';this.setHomePage(' http://www.yes1000.com');/" >设为首页</div>"
strMenu += "<div class=/"menuitems/" onClick=/"javascript:window.external.AddFavorite(' http://www.yes1000.com', '::一想千开::')/">加入收藏</div>"
strMenu += "<hr>"
strMenu += "<div class=/"menuitems/" onClick=/"javascript:location.href=' http://www.yes1000.com'/">网站首页</div>"
strMenu += "<div class=/"menuitems/" onClick=/"javascript:location.href=' http://www.yes1000.com/default.asp?cateID=3'/">杂七杂八</div>"
strMenu += "<div class=/"menuitems/" onClick=/"javascript:location.href=' http://www.yes1000.com/default.asp?cateID=5'/">技术文档</div>"
strMenu += "<div class=/"menuitems/" onClick=/"javascript:location.href=' http://www.yes1000.com/default.asp?cateID=10'/">网络杂谈</div>"
strMenu += "<div class=/"menuitems/" onClick=/"javascript:location.href=' http://www.yes1000.com/default.asp?cateID=6'/">原创专区</div>"
strMenu += "<div class=/"menuitems/" onClick=/"javascript:location.href=' http://www.yes1000.com/default.asp?cateID=12'/"> 经典资源</div>"
strMenu += "<hr>"
strMenu += "<div class=/"menuitems/" onClick=/"javascript:location.href=' http://www.yes1000.com/LoadMod.asp?plugins=AboutMeForPJBlog'/">个人档案</a></div>"
strMenu += "<hr>"
strMenu += "<div class=/"menuitems/" onClick=/"javascript:location.href=' http://www.yes1000.com/article.asp?id=78'/">和我联系</a></div>"
strMenu += "</div>"

// 判断客户端浏览器
function ie() {
    if (navigator.appName=="Microsoft Internet Explorer") {
        return true;
    } else {
        return false
}}

// 显示菜单
function showmenu(){
    if (ie()){
    // 找出鼠标在窗口中的位置
    var rightedge=document.body.clientWidth-event.clientX
    var bottomedge=document.body.clientHeight-event.clientY

    // 如果横向的距离小于菜单的宽度
    if (rightedge<menu.offsetWidth)
        // 将菜单向左移动到适当的位置
        menu.style.left=document.body.scrollLeft+event.clientX-menu.offsetWidth
    else
        // 否则,就在该位置显示菜单
        menu.style.left=document.body.scrollLeft+event.clientX

    // 与上面道理相同,判断纵向的位置
    if (bottomedge<menu.offsetHeight)
        menu.style.top=document.body.scrollTop+event.clientY-menu.offsetHeight
    else
        menu.style.top=document.body.scrollTop+event.clientY

        menu.style.visibility="visible"
    }
    return false
}


// 隐藏菜单
function hidemenu(){
    if (ie()) menu.style.visibility="hidden"
}

// 菜单项获得焦点时加亮显示
function highlight(){
    if (event.srcElement.className=="menuitems"){
        event.srcElement.style.backgroundColor="highlight"
        event.srcElement.style.color="highlighttext"
}}

// 菜单项失去焦点
function lowlight(){
    if (event.srcElement.className=="menuitems"){
    event.srcElement.style.backgroundColor=""
    event.srcElement.style.color="menutext"
}}

if (ie()) document.write (strMenu);
document.oncontextmenu= showmenu
document.body.onclick= hidemenu


必不可少的CSS:
程序代码

.clsMenu {
    BORDER-RIGHT: buttonhighlight 2px outset;PADDING-top: 5px; BORDER-TOP: buttonhighlight 2px outset; VISIBILITY: hidden; BORDER-LEFT: buttonhighlight 2px outset; WIDTH: 135px; CURSOR: default; COLOR: menutext; BORDER-BOTTOM: buttonhighlight 2px outset; POSITION: absolute; BACKGROUND-COLOR: menu
}
.menuitems {
    PADDING-RIGHT: 10px; PADDING-LEFT: 20px; FONT-SIZE: 12px; COLOR: #000000; LINE-HEIGHT: 18px; FONT-FAMILY: "宋体"; TEXT-DECORATION: none
}


JS必须在body标记内调用。 
 

你可能感兴趣的:(function,Microsoft,IE,Class,div,internet)