最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js制作提示框插件

 更新時(shí)間:2020年12月24日 10:43:27   作者:lanshanxiao  
這篇文章主要介紹了js制作提示框插件的方法,幫助大家更好的理解和使用js,感興趣的朋友可以了解下

JavaScript制作一個(gè)簡單的提示框插件

下面是制作的提示框插件文件

window.myPlugin = window.myPlugin || {};

window.myPlugin.showMsg = (function () {
  var mongolia, //蒙層
    promptBox, //提示框
    closeSpan, //關(guān)閉按鈕
    titleSpan, //提示標(biāo)題
    contextSpan, //提示信息
    okBtn, //確定按鈕
    cancelBtn, //取消按鈕
    isRegEvent, //是否注冊事件
    option; //傳入的參數(shù)



  /**
   * 初始化蒙層
   */
  function initMongolia() {
    if (!mongolia) { //沒有蒙層則初始化
      //蒙層:覆蓋整個(gè)窗口,半透明的黑色
      mongolia = document.createElement("div");
      mongolia.style.position = "fixed";
      mongolia.style.width = mongolia.style.height = "100%";
      mongolia.style.left = mongolia.style.top = 0;
      mongolia.style.background = "rgba(0,0,0,.5)";
      document.body.appendChild(mongolia);
    }
    mongolia.style.display = "block"; //展示蒙層
  }

  /**
   * 初始化提示框
   */
  function initPromptBox() {
    //提示框:寬高300,位置居中
    if (!promptBox) {
      promptBox = document.createElement("div");
      promptBox.style.width = promptBox.style.height = "300px";
      promptBox.style.background = "#fff";
      promptBox.style.fontSize = "14px";
      promptBox.style.position = "absolute";
      promptBox.style.top = promptBox.style.left = "50%";
      promptBox.style.marginLeft = promptBox.style.marginTop = "-150px";
      promptBox.style["data-myplugin-id"] = "promptBox";
      initPromptContext();
      mongolia.appendChild(promptBox);
      titleSpan = document.querySelector("[data-myplugin-id='title']"); //提示標(biāo)題
      contextSpan = document.querySelector("[data-myplugin-id='message']"); //提示信息
      closeSpan = document.querySelector("[data-myplugin-id='close']"); //關(guān)閉按鈕
      okBtn = document.querySelector("[data-myplugin-id='ok']"); //確定按鈕
      cancelBtn = document.querySelector("[data-myplugin-id='cancel']"); //取消按鈕
    }

    okBtn.innerText = option.okText || "確定";
    cancelBtn.innerText = option.cancelText || "取消";
    titleSpan.innerText = option.title || "提示";
    contextSpan.innerText = option.context || "";
  }

  /**
   * 初始化提示框中的內(nèi)容
   */
  function initPromptContext() {
    //內(nèi)容包含:標(biāo)題,關(guān)閉按鈕,提示信息,確定按鈕,取消按鈕

    //創(chuàng)建標(biāo)題,關(guān)閉按鈕
    var div = document.createElement("div");
    div.innerHTML = `<span style="float:left;" data-myplugin-id="title"></span>
    <span style="float:right;cursor:pointer;" data-myplugin-id="close">X</span>`;
    div.style.height = "50px";
    div.style.padding = "10px 20px";
    div.style.background = "#eee";
    div.style.boxSizing = "border-box";
    promptBox.appendChild(div);

    //創(chuàng)建提示信息
    div = document.createElement("div");
    div.innerHTML = `<span data-myplugin-id="message"></span>`;
    div.style.height = "200px";
    div.style.padding = "10px 20px";
    div.style.boxSizing = "border-box";
    promptBox.appendChild(div);

    //創(chuàng)建確定按鈕,取消按鈕
    div = document.createElement("div");
    div.innerHTML = `<button style="float:right;margin:10px;cursor:pointer;" data-myplugin-id="cancel"></button><button style="float:right;margin:10px;cursor:pointer;" data-myplugin-id="ok"></button>`;
    div.style.height = "50px";
    div.style.padding = "10px 20px";
    div.style.boxSizing = "border-box";
    promptBox.appendChild(div);
  }

  //注冊事件
  function regEvent() {
    if (!isRegEvent) { //未注冊事件
      //1.點(diǎn)擊關(guān)閉,點(diǎn)擊蒙層,點(diǎn)擊取消按鈕
      closeSpan.onclick = mongolia.onclick = function () {
        mongolia.style.display = "none"; //隱藏蒙層
      };

      okBtn.onclick = function () {
        option && option.okFunction && option.okFunction();
        mongolia.style.display = "none"; //隱藏蒙層
      }

      cancelBtn.onclick = function () {
        option && option.cancelFunction && option.cancelFunction();
        mongolia.style.display = "none"; //隱藏蒙層
      }

      //2.拖動提示框事件
      window.onmousedown = function (e) {
        var target = getTarget(e.target); //是否包含目標(biāo)元素

        if (target) {
          var style = window.getComputedStyle(target);
          var left = parseInt(style.left);
          var top = parseInt(style.top);
          var disX = parseInt(e.pageX) - left;
          var disY = parseInt(e.pageY) - top;

          window.onmousemove = function (e) {
            var newLeft = parseInt(e.pageX) - disX;
            var newTop = parseInt(e.pageY) - disY;

            promptBox.style.left = newLeft + "px";
            promptBox.style.top = newTop + "px";

          };
          window.onmouseup = window.onmouseleave = function () {
            window.onmousemove = null;
          }
        }
      };

      function getTarget(target) {
        while (target) {
          if (target.tagName === "DIV" && target.style["data-myplugin-id"] === "promptBox") {
            return target;
          } else {
            target = target.parentElement;
          }
        }
        return false;
      }
    }
  }




  /**
   * @param {object} opts 
   * opts.title : 提示標(biāo)題
   * opts.context : 提示信息
   * opts.cancelText:取消按鈕內(nèi)容
   * opts.okText:確定按鈕內(nèi)容
   * opts.cancelText:取消按鈕內(nèi)容
   * opts.okFunction:確定按鈕的回調(diào)函數(shù)
   * opts.cancelFunction:取消按鈕的回調(diào)函數(shù)
   */
  function showMsg(opts) {
    if (typeof opts === "string") {
      option = {
        context: opts
      }
    } else {
      option = opts || {};
    }
    initMongolia();
    initPromptBox();
    regEvent();
  }

  return showMsg;
}());

myPlugin.js

引入并使用myPlugin.js文件

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script src="./js/myPlugin.js"></script>
  <script>
    window.myPlugin.showMsg({
      title: "信息",
      context: "確定刪除嗎",
      okText: "OK",
      cancelText: "Cancel",
      okFunction: function(){
        console.log("點(diǎn)擊OK按鈕");
      },
      cancelFunction:function(){
        console.log("點(diǎn)擊Cancel按鈕");
      }
    });
  </script>
</body>

</html>

index.html

效果展示:

以上就是js制作提示框插件的詳細(xì)內(nèi)容,更多關(guān)于js 制作提示框的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Laravel中常見的錯誤與解決方法小結(jié)

    Laravel中常見的錯誤與解決方法小結(jié)

    大家在用Laravel框架期間可能會遇到了不少問題,現(xiàn)在我將自己遇到的一些問題總結(jié)出來,有一些調(diào)試起來著實(shí)不太容易,本文篩選出幾個(gè),希望這篇文章能讓大家在PHP開發(fā)中少走一些彎路。
    2016-08-08
  • JavaScript實(shí)現(xiàn)帶播放列表的音樂播放器實(shí)例分享

    JavaScript實(shí)現(xiàn)帶播放列表的音樂播放器實(shí)例分享

    這篇文章主要介紹了JavaScript實(shí)現(xiàn)帶播放列表的音樂播放器實(shí)例分享,包括對播放完歌單之后沒有將要播放的歌曲的提示功能,需要的朋友可以參考下
    2016-03-03
  • JavaScript的各種常見函數(shù)定義方法

    JavaScript的各種常見函數(shù)定義方法

    這篇文章主要介紹了JavaScript的各種常見函數(shù)定義方法,包含了一些技巧的測試與分析總結(jié),需要的朋友可以參考下
    2014-09-09
  • javascript replace()方法的簡單分析

    javascript replace()方法的簡單分析

    javascript中replace()在javascript中,String的函數(shù)replace()簡直太讓人喜愛了。它靈活而強(qiáng)大的字符替換處理能力,讓我不禁想向大家介紹它。
    2008-11-11
  • Fullpage.js固定導(dǎo)航欄-實(shí)現(xiàn)定位導(dǎo)航欄

    Fullpage.js固定導(dǎo)航欄-實(shí)現(xiàn)定位導(dǎo)航欄

    FullPage.js 是一個(gè)簡單而易于使用的插件,用來創(chuàng)建全屏滾動網(wǎng)站(也被稱為單頁網(wǎng)站)。接下來通過本文給大家介紹Fullpage.js固定導(dǎo)航欄-實(shí)現(xiàn)定位導(dǎo)航欄,對fullpage.js導(dǎo)航欄相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧
    2016-03-03
  • 如何在selenium中使用js實(shí)現(xiàn)定位

    如何在selenium中使用js實(shí)現(xiàn)定位

    這篇文章主要介紹了如何在selenium中使用js實(shí)現(xiàn)定位,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • JS+AJAX實(shí)現(xiàn)省市區(qū)的下拉列表聯(lián)動

    JS+AJAX實(shí)現(xiàn)省市區(qū)的下拉列表聯(lián)動

    這篇文章主要為大家詳細(xì)介紹了JS+AJAX實(shí)現(xiàn)省市區(qū)的下拉列表聯(lián)動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 點(diǎn)擊button獲取text內(nèi)容并改變樣式的js實(shí)現(xiàn)

    點(diǎn)擊button獲取text內(nèi)容并改變樣式的js實(shí)現(xiàn)

    這篇文章主要介紹了點(diǎn)擊button獲取text內(nèi)容并改變樣式的js實(shí)現(xiàn),經(jīng)測試非常實(shí)用,需要的朋友可以參考下
    2014-09-09
  • 詳解JavaScript是如何驗(yàn)證URL的

    詳解JavaScript是如何驗(yàn)證URL的

    當(dāng)開發(fā)者需要為不同目的以不同形式處理URL時(shí),我們經(jīng)常會借助于JavaScript。本文就為大家整理了JavaScript驗(yàn)證URL的方法,希望對大家有所幫助
    2023-02-02
  • 實(shí)例講解避免javascript沖突的方法

    實(shí)例講解避免javascript沖突的方法

    這篇文章主要以實(shí)例的方式講解了避免javascript沖突的方法,具有一定的參考價(jià)值,感興趣的朋友可以參考一下
    2016-01-01

最新評論

随州市| 山阴县| 永康市| 昌吉市| 老河口市| 余干县| 南投市| 泰兴市| 东丽区| 遂川县| 教育| 永州市| 正定县| 东明县| 嘉鱼县| 大邑县| 彭阳县| 永和县| 扶余县| 保德县| 天津市| 颍上县| 福安市| 明星| 阜康市| 城市| 仁布县| 霞浦县| 图们市| 安泽县| 长垣县| 高阳县| 镇康县| 卫辉市| 贵州省| 西丰县| 醴陵市| 阿克苏市| 从化市| 盐池县| 呼伦贝尔市|