网页中有一个Div,怎么设置DIV位于页面中央(水平中央和垂直都要中央)

可以设置body为一个弹性盒子(display: flex), div设置 margin: auto;

flex-grow属性

flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。

  1. .item {
  2. flex-grow: <number>; /* default 0 */
  3. }

如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。
注意:是剩余空间。

移动端事件

Meta对象:
Meta 对象代表 HTML 的 一个 元素。
元素可提供有关某个 HTML 元素的元信息 (meta-information),比如描述、针对搜索引擎的关键词以及刷新频率。

  1. <meta name="keywords" content="HTML, DHTML, CSS, XHTML, JavaScript">
  2. <meta name="description" content="Free Web tutorials">
  3. <meta name="author" content="Hege Refsnes">
  4. <meta http-equiv="Content-Type" content="text/html;charset=utf-8">

元数据(metadata)元素的概念,用来构建HTML文档的基本结构,以及就如何处理文档向浏览器提供信息和指示,它们本身不是文档内容,但提供了关于后面文档内容的信息。——出自《html5权威指南》。

元素出去charset属性外,都是http-equiv属性或name属性结合content来使用。

指定名/值对定义元数据

name属性与content属性结合使用, name用来表示元数据的类型,表示当前标签的具体作用;content属性用来提供值。

  1. <meta name="参数" content="具体描述信息">

例如:

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>demo</title>
  5. <meta name="keywords" content="电商,物流">
  6. <meta name="author" content="张三">
  7. <meta name="description" content="网站描述...">
  8. </head>
  9. <body>
  10. <div>welcome</div>
  11. </body>
  12. </html>
元数据名称(name的值) 说明
application name 当前页所属Web应用系统的名称
keywords 描述网站内容的关键词,以逗号隔开,用于SEO搜索
description 当前页的说明
author 当前页的作者名
copyright 版权信息
renderer renderer是为双核浏览器准备的,用于指定双核浏览器默认以何种方式渲染页面
viewreport 它提供有关视口初始大小的提示,仅供移动设备使用

image.png

charset属性为HTML5新增的属性,用于声明字符编码,以下两种写法效果一样

  1. <meta charset="utf-8"> //HTML5
  1. <meta http-equiv="content-Type" content="text/html;charset=utf-8"> //旧的HTML

http-equiv属性与content属性结合使用, http-equiv属性为指定所要模拟的标头字段的名称,content属性用来提供值。

  1. <meta http-equiv="参数" content="具体的描述">

content-Type 声明网页字符编码:

  1. <meta http-equiv="content-Type" content="text/html charset=UTF-8">

https://segmentfault.com/a/1190000010342600

image.png

应当使用touchstart,避免使用click,click事件有一定延迟,延迟300ms。

image.png
示例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scaleable: no">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style>
  9. html, body{
  10. height: 100%;
  11. }
  12. .con{
  13. width: 300px;
  14. height: 200px;
  15. background-color: green;
  16. color: white;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <h1>移动端事件</h1>
  22. <button id="touchstart">Click</button>
  23. <div class="con" id="con">
  24. <p id="p"></p>
  25. </div>
  26. </body>
  27. <script>
  28. function $my(id){
  29. return document.getElementById(id);
  30. }
  31. var btn = $my('touchstart');
  32. btn.addEventListener('touchstart', (e)=>{
  33. console.log('touch start');
  34. });
  35. var con = $my('con');
  36. var p = $my('p');
  37. let i = 0;
  38. let time = null;
  39. con.addEventListener('touchmove', (e)=>{
  40. // 事件节流
  41. if (!time){
  42. time = setTimeout(()=>{
  43. p.innerHTML = e.target.tagName + ', ' + i++;
  44. time = null;
  45. }, 1000);
  46. }
  47. console.log('事件开始');
  48. });
  49. // btn.addEventListener('click', (e)=>{
  50. // console.log('click');
  51. // });
  52. </script>
  53. </html>

示例代码:

  1. (function(window){
  2. // 对外提供的接口
  3. function myMobile(selector){
  4. return myMobile.prototype._init(selector);
  5. }
  6. myMobile.prototype = {
  7. // 初识方法
  8. _init: function(selector){
  9. if(typeof selector === 'string') {
  10. this.ele = window.document.querySelector(selector);
  11. return this;
  12. }
  13. },
  14. // 单击事件
  15. tap: function(handler){
  16. this.ele.addEventListener('touchstart', touchFn);
  17. this.ele.addEventListener('touchend', touchFn);
  18. var startTime, endTime;
  19. function touchFn(e) {
  20. e.preventDefault();
  21. var currentDate = new Date().getTime();
  22. switch(e.type) {
  23. case 'touchstart':
  24. startTime = currentDate;
  25. break;
  26. case 'touchend':
  27. endTime = currentDate;
  28. if (endTime - startTime < 300){
  29. handler.call(this, e);
  30. }
  31. break;
  32. }
  33. }
  34. },
  35. // 长按事件
  36. longTap: function(handler){
  37. this.ele.addEventListener('touchstart', touchFn);
  38. this.ele.addEventListener('touchmove', touchFn);
  39. this.ele.addEventListener('touchend', touchFn);
  40. var timerId;
  41. function touchFn(e){
  42. switch(e.type) {
  43. case 'touchstart':
  44. timerId = setTimeout(()=>{
  45. handler.call(this, e);
  46. }, 1000);
  47. break;
  48. case 'touchmove':
  49. clearTimeout(timerId);
  50. break;
  51. case 'touchend':
  52. clearTimeout(timerId);
  53. break;
  54. }
  55. }
  56. },
  57. // 左滑事件
  58. slideLeft: function(handler){
  59. this.ele.addEventListener('touchstart', touchFn);
  60. this.ele.addEventListener('touchend', touchFn);
  61. var startX, startY, endX, endY;
  62. function touchFn(e){
  63. e.preventDefault();
  64. //屏幕上正在移动的点,第一个点
  65. var firstTouch = e.changedTouches[0];
  66. switch(e.type) {
  67. case 'touchstart':
  68. startX = firstTouch.pageX;
  69. startY = firstTouch.pageY;
  70. break;
  71. case 'touchend':
  72. endX = firstTouch.pageX;
  73. endY = firstTouch.pageY;
  74. // X方向移动的距离大于Y,定义用户操作左滑操作;30:移动的距离
  75. if(Math.abs(endX - startX) >= Math.abs(endY - startY) && startX - endX >= 30){
  76. handler.call(this, e);
  77. }
  78. break;
  79. }
  80. }
  81. }
  82. };
  83. window.$ = window.myMobile = myMobile;
  84. })(window);
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <script src="./h5.js"></script>
  9. <style>
  10. html,body{
  11. height: 100%;
  12. }
  13. .con{
  14. width: 300px;
  15. height: 200px;
  16. background: green;
  17. color: white;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <h1>移动端事件</h1>
  23. <button>
  24. 单击事件
  25. </button>
  26. <div class="con">
  27. </div>
  28. </body>
  29. <script>
  30. $('button').tap(function(e){
  31. console.log('单击事件');
  32. });
  33. $('button').longTap(function(e){
  34. console.log('长按事件');
  35. });
  36. $('div').slideLeft(function(e){
  37. console.log('左滑事件');
  38. });
  39. </script>
  40. </html>