JavaScript模擬鼠標(biāo)右鍵菜單效果
來源:易賢網(wǎng) 閱讀:752 次 日期:2016-08-02 15:57:44
溫馨提示:易賢網(wǎng)小編為您整理了“JavaScript模擬鼠標(biāo)右鍵菜單效果”,方便廣大網(wǎng)友查閱!

本文實(shí)例為大家分享了JavaScript模擬鼠標(biāo)右鍵菜單的具體代碼,供大家參考,具體代碼如下

具體代碼:

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <title>右鍵菜單</title>

  <style type="text/css">

    *{

      margin: 0;

      padding: 0;

    }

    #menu{

      width: 254px;

      /*background-color: #ccc;*/

      font-size: 12px;

      position: fixed;

      top: 0px;

      left: 0px;

      /*height: 240px;*/

      /*padding-left: 26px;*/

      padding-top: 2px;

      border:1px solid #ccc;

      display: none;

    }

    #menu li{

      list-style: none;

      line-height: 25px;

      margin-left: -1px;

      padding-left: 26px;

    }

    #menu li:hover{

      background-color: #4281f4;

      color: #fff;

    }

  </style>

</head>

<body>

   <ul id="menu">

     <li>返回(B)</li>

     <li>前進(jìn)(F)</li>

     <li>從新加載(R)</li>

     <li>另存為(A)</li>

     <li>打印(P)</li>

     <li>查看網(wǎng)頁源代碼(V)</li>

     <li>查看網(wǎng)頁信息(I)</li>

     <li>審查元素(N)</li>

   </ul>

   <script type="text/javascript">

    var menu = document.getElementById("menu");

    document.oncontextmenu =function(e){

      var e = e ||window.event;//兼容

     console.log(e.clientX,e.clientY);

     console.log(e);

     //單擊顯示div

     menu.style.display = "block";

     //設(shè)置定義

     //判斷鼠標(biāo)坐標(biāo)是否大于視口寬度-塊本身寬度

     var cakLeft = (e.clientX > document.documentElement.clientWidth - menu.offsetWidth)?(document.documentElement.clientWidth - menu.offsetWidth):e.clientX;

     var cakTop = (e.clientY > document.documentElement.clientHeight - menu.offsetHeight)?(document.documentElement.clientHeight - menu.offsetHeight):e.clientY;

     menu.style.left = cakLeft + "px";

     menu.style.top = cakTop + "px";

    return false;

    }

      menu.onclick = function(e) {

    var e = e || window.event;

    e.cancelBubble = true;

    //阻止冒泡。

  }

   document.onclick = function() {

    menu.style.display = "none";

  }

   </script>

</body>

</html>

希望本文所述對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:JavaScript模擬鼠標(biāo)右鍵菜單效果
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)