送你一个封装的移动端横屏展示的JS库

简介: 我们经常在移动端横屏展示一些东西,所以今天我们来介绍下怎么在移动端实现横屏展示。

实现


源码:


// screenOrientation.js
export default function (option) {
  var _this = this;
  _this.option = {
    'mode': 'portrait', //portrait(竖屏)、landscape(横屏)
    'id': 'wrap', //最外层容器ID
    'init': false,
    'callback': false
  };
  for (var k in option)
    if (option[k] != '') _this.option[k] = option[k];
  var obj = document.getElementById(_this.option.id);
  var className = obj.className || " ";
  var w_width = obj.clientWidth;
  var w_height = obj.clientHeight;
  var flag = 0;
  var timer;
  if (_this.option.mode == "portrait")
    var cssBlock =
      '.' + _this.option.id + '_screenOrientation{-webkit-transform: rotateZ(-90deg) !important; transform: rotateZ(-90deg); position:relative;}';
  else
    // eslint-disable-next-line no-redeclare
    var cssBlock =
      '.' + _this.option.id + '_screenOrientation{-webkit-transform: rotateZ(90deg) !important; transform: rotateZ(90deg); position:relative;}';
  var style = document.createElement("style");
  style.type = "text/css";
  style.textContent = cssBlock;
  document.getElementsByTagName("head").item(0).appendChild(style);
  var _width = (window.innerWidth > 0) ? window.innerWidth : screen.width;
  var _height = (window.innerHeight > 0) ? window.innerHeight : screen.height;
  _this.modifyDetec = function () {
    if ((_height < _width && _this.option.mode == "portrait") || (_height > _width && _this.option.mode == "landscape") && flag == 0) {
      obj.style.width = _height + "px";
      obj.style.height = _width + "px";
      obj.style.top = -(_width - _height) * 0.5 + "px";
      obj.style.left = -(_height - _width) * 0.5 + "px";
      obj.className = className + " " + (_this.option.id + "_screenOrientation");
      flag = 1;
      if (_this.option.callback) {
        _this.option.callback();
      }
    } else {
      obj.style.width = w_width + "px";
      obj.style.height = w_height + "px";
      obj.className = className;
      obj.style.top = " ";
      obj.style.left = " ";
      flag = 0;
    }
  }
  function detectOtt() {
    _width = (window.innerWidth > 0) ? window.innerWidth : screen.width;
    _height = (window.innerHeight > 0) ? window.innerHeight : screen.height;
    if (window.orientation == 180 || window.orientation == 0 || window.orientation == 90 || window.orientation == -90) {
      clearTimeout(timer);
      timer = setTimeout(function () {
        _this.modifyDetec(_width, _height);
      }, 50);
    }
  }
  if (_this.option.init) {
    _this.option.init();
  }
  detectOtt();
  window.addEventListener("resize", detectOtt, false);
}


使用:


import screenOrientation from './screenOrientation.js'
// 方法
screen(){
  // portrait(竖屏)、landscape(横屏)
  // id : 最外层容器DOM元素ID
  new screenOrientation({ mode:'landscape', id:'wrap'})
}


结语




相关文章
|
20天前
sd.js 2.0封装:更加简化请求传参内容(逐步废弃、逐渐日落2024.01.02)
sd.js 2.0封装:更加简化请求传参内容(逐步废弃、逐渐日落2024.01.02)
|
8天前
|
前端开发 JavaScript 区块链
连接区块链节点的 JavaScript 库 web3.js
连接区块链节点的 JavaScript 库 web3.js
|
19天前
|
移动开发 前端开发 JavaScript
前端高效开发JavaScript库!
前端高效开发JavaScript库!
|
6天前
|
JavaScript 前端开发
JavaScript实现缓慢动画的封装
JavaScript实现缓慢动画的封装
10 0
|
6天前
|
JavaScript Java 测试技术
基于springboot+vue.js的精品在线试题库系统附带文章和源代码设计说明文档ppt
基于springboot+vue.js的精品在线试题库系统附带文章和源代码设计说明文档ppt
21 6
|
6天前
|
JavaScript Java 测试技术
学习资料库小程序设计ssm+vue.js附带文章和源代码设计说明文档ppt
学习资料库小程序设计ssm+vue.js附带文章和源代码设计说明文档ppt
12 1
|
20天前
|
JavaScript 前端开发 API
在Node.js上使用dojo库进行面向对象web应用开发
请注意,虽然这个例子在Node.js环境中使用了Dojo,但Dojo的许多功能(例如DOM操作和AJAX请求)在Node.js环境中可能无法正常工作。因此,如果你打算在Node.js环境中使用Dojo,你可能需要查找一些适用于服务器端JavaScript的替代方案。
20 0
|
20天前
|
JavaScript 前端开发 IDE
【JavaScript与TypeScript技术专栏】TypeScript在JavaScript库与框架开发中的作用
【4月更文挑战第30天】TypeScript,微软开发的JavaScript超集,以其强类型和面向对象特性,正成为提升Web项目质量和效率的关键工具,尤其在库和框架开发中。它通过类型系统减少运行时错误,提供内置文档,便于重构,增强IDE支持,以及支持模块化。流行框架如React、Angular已支持TypeScript,未来有望成为开发高质量库和框架的标准语言。随着社区增长,TypeScript将在Web开发领域扮演更重要角色。
|
20天前
|
数据可视化 JavaScript 前端开发
Echarts是一个开源的JavaScript可视化库,用于创建各种类型的图表
Echarts是JavaScript的开源可视化库,Python通过Pyecharts库可调用它来绘制图表。示例展示了如何用Pyecharts创建柱状图:定义图表对象,设置标题和坐标轴,添加X轴、Y轴数据,最后渲染展示。Pyecharts还支持折线图、散点图、饼图等多种图表类型,更多详情可查阅官方文档。
37 0
|
20天前
|
SQL 存储 前端开发
React&Nest.js全栈社区平台(五)——👋封装通用分页Service实现文章流与详情
React&Nest.js全栈社区平台(五)——👋封装通用分页Service实现文章流与详情
React&Nest.js全栈社区平台(五)——👋封装通用分页Service实现文章流与详情