js 阻止移动端横屏工具方法

参考文章链接

直接引入js文件即可

// 阻止移动端横屏 弹出提示 
(function rotate() {
  var orientation = window.orientation;
  var pd = null;
  function createPd() {
    if (document.getElementById("preventTran") === null) {
      var imgData =
        "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADwAAABaCAYAAADkUTU1AAAI9ElEQVR4Xu1cfXBcVRU/5+Z1N8GEj2AhFQvUIigfBetYaRVbBhADU2wHVoYk3bx3k8kMcSyFPxzUf8IfOjrqIHYUXbL3vW6mKXbtINapg1ColLEUnYIj9QPGOE0VdUjjlE3tdnffO87J7GY26yZ9H5tNst37X5tzzu/87rl777v3nnMR5rhFo9HLhBDrhRC3AMBqAFgBABfmYU8CwAgAHAGAVwDgJaXUO+Vc6u7uXhkOh0/GYrGxIC5jEOVZdLG3t7fdcZyHiOgORHSL4xDRfiHEE/F4fB8AEGNIKdcS0fMA8IxpmluC+OzWEdcY0Wh0jaZp2wFgjWulMoJE9CoRbRVCEHcCIp4PAOOpVOqSZDJp+7VdMcIbNmzQVqxYMYCIXwEA4dehEj2O+GlEfF/h/xFxfTwef9mv/YoQ7u/vb06n00kA+FypIxweAHgdAJ4DgF9nMpmj4+Pj77Jca2vr0nA4fC0ArAeAO4lotYvh/22l1JfnjXAkEmluaWn5JQB8ukx09hLRgGVZb7hxUNf1m4QQjxHRxlmI/0kpxZ3kqwWNMEopfwIAkRL0fwNAn1Lq51696ujouKKxsfEwAFw6k246nV45PDzMs7vnFoiwlPIRAPhuCeqbjuPcYVnWv7x609nZ+cFwOMzL0xVn0d2qlOKJ0XPzTZjXxYaGhqMAEC5C/aOmaetisRivr55aV1fXsiVLlhxExJVnU+QlyjTNz55NrtzffROWUj4DAJuKjI4j4up4PH7MjyOGYTyNiPe70SWiDCK+XymVciNfLOOLcDQaXaVpGk9EU/qO40Qtyxry6kBB3jCMpUQUEUJsIKIbEPEqANBmsseypmn+1CueL8JSyh8AQH8BjIiOmKb5ca/gs8l3dnae39jYeJfjODxjXw8APN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9znfAAAAAElFTkSuQmCC";
      pd = document.createElement("div");
      pd.setAttribute("id", "preventTran");
      pd.style.position = "fixed";
      pd.style.left = "0";
      pd.style.top = "0";
      pd.style.width = "100%";
      pd.style.height = "100vh";
      pd.style.overflow = "hidden";
      pd.style.backgroundColor = "#2e2e2e";
      pd.style.textAlign = "center";
      pd.style.display = "flex";
      pd.style.flexDirection = "column";
      pd.style.justifyContent = "center";
      pd.style.alignItems = "center";
      document.getElementsByTagName("body")[0].appendChild(pd);
      var img = document.createElement("img");
      img.src = imgData;
      pd.appendChild(img);
      img.style.marginBottom = "0.2rem";
      img.style.width = "10%";
      var br = document.createElement("br");
      var p = document.createElement("p");
      p.style.width = "100%";
      p.style.height = "auto";
      p.style.fontSize = "0.25rem";
      p.style.color = "#626262";
      p.style.lineHeight = "0.38rem";
      p.style.textAlign = "center";
      p.innerHTML = "为了您的良好体验";
      p.appendChild(br);
      p.innerHTML += "请将手机/平板竖屏操作";
      pd.appendChild(p);
    }
  }
  if (orientation == 90 || orientation == -90) {
    if (pd == null && document.getElementById("preventTran") === null)
      createPd();
    document.getElementById("preventTran").style.zIndex = "9999";
  }
  //H5新特性 监听用户水平或垂直翻转设备(即方向发生改变)
  window.onorientationchange = function() {
    if (pd == null && document.getElementById("preventTran") == null)
      createPd();
    document.getElementById("preventTran").style.zIndex = "-1";
    rotate();
  };
})();

上一篇:Python计算每个班级的前三名


下一篇:pandasgui