用javascript 怎样才能很好的获取手机的屏幕宽度和高度

JavaScript09

用javascript 怎样才能很好的获取手机的屏幕宽度和高度,第1张

一般来说在移动浏览器上是拿不到屏幕真实分辨率的,因为页面渲染是在一个叫viewport的页面绘制区域内(说的通俗点就是手机浏览器是用一个虚拟的屏幕来显示网页的)。viewport和屏幕的真实尺寸并不是对应的,如在Safari Mobile中viewport默认宽度(320px)是屏幕真实尺寸(640px)的一半,这里不管是用window.innerHeight还是window.screen.width拿到的都是320px。当然我们可以通过meta设置改变viewport的比例,如initial-scale=.5就可以让viewport和屏幕一致的尺寸,但这个比例在不同的手机上并不一定是2倍关系,特别是Android手机,在大屏手机(5寸以上)上这个值是3倍甚至4倍。而且改变viewport比例后可能会导致后续制作中出现一系列问题。所以我认为不要纠结一定要拿到屏幕真实尺寸,就把viewport的尺寸当成屏幕的尺寸进行页面设计和制作肯定是没有问题的。

有一个非常曲线的解决办法,就是:css的媒体查询(@media)是能够检测屏幕尺寸(其实是浏览器窗体的真实尺寸,并不是屏幕的真实物理尺寸,介意的朋友就不要看了)的,通过它来给网页内的某个元素设置一个特殊的属性,然后再用JavaScript来获取这个属性值。当然这样只能获得一个阶梯值(比如480px到540px之间、540px到600px之间等等),不是精确值,所以可能得不偿失,因此我是不推荐的。

Javascript:

网页可见区域宽: document.body.clientWidth

网页可见区域高: document.body.clientHeight

网页可见区域宽: document.body.offsetWidth (包括边线的宽)

网页可见区域高: document.body.offsetHeight (包括边线的高)

网页正文全文宽: document.body.scrollWidth

网页正文全文高: document.body.scrollHeight

网页被卷去的高: document.body.scrollTop

网页被卷去的左: document.body.scrollLeft

网页正文部分上: window.screenTop

网页正文部分左: window.screenLeft

屏幕分辨率的高: window.screen.height

屏幕分辨率的宽: window.screen.width

屏幕可用工作区高度: window.screen.availHeight

屏幕可用工作区宽度: window.screen.availWidth

JQuery:

$(document).ready(function(){

alert($(window).height())//浏览器当前窗口可视区域高度

alert($(document).height())//浏览器当前窗口文档的高度

alert($(document.body).height())//浏览器当前窗口文档body的高度

alert($(document.body).outerHeight(true))//浏览器当前窗口文档body的总高度 包括border padding margin

alert($(window).width())//浏览器当前窗口可视区域宽度

alert($(document).width())//浏览器当前窗口文档对象宽度

alert($(document.body).width())//浏览器当前窗口文档body的宽度

alert($(document.body).outerWidth(true))//浏览器当前窗口文档body的总宽度 包括border padding margin

})

一、获取选中的文字

使用window.getSelection().toString()方法来获取选中的文字,在选中文字鼠标松开后会获取到选中的文字:

<p>可以选中一些文本</p>

<script type="text/javascript">

let selected = window.getSelection().toString()

console.log(selected)

if(selected != '' &&selected != null){

window.alert('要百度搜索吗?')

}

</script>

1

2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9

二、让内容可编辑

第一步:为元素设置contenteditable属性并赋值为true,让元素具有可编辑功能,当将其值赋值为false时不可编辑;

第二步:伪元素设置spellcheck属性,并赋值为true,即开启拼写检查,设置值为false时关闭拼写检查

**注意:**浏览器定义了多文本编辑命令,使用dicument,execCommand()可以调用(比如copy,selectAll等命令;在使用execCommand()方法时,界面元素的contenteditable属性值不能设置为true,否则会影响copy命令)

<div contenteditable="true" spellcheck="true"></div>

<button>提交</button>

<script type="text/javascript">

let div = document.querySelector('div')

let btn = document.querySelector('button')

btn.onclick = function(){

console.log(div.innerText)

}

</script>

1

2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9

三、JS动画

原理:通过定时器setInterval()不断移动盒子位置。

例:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

<style type="text/css">

.box1{

width: 200px

height: 200px

position: absolute

top: 50px

background-color: #3B78DD

}

.box2{

width: 100px

height: 100px

position: absolute

top: 400px

background-color: lightblue

}

button{

position: absolute

top: 300px

}

</style>

</head>

<body>

<div class="box1"></div>

<div class="box2"></div>

<button>点击移动box2</button>

<script type="text/javascript">

let box1 = document.querySelector('.box1')

let box2 = document.querySelector('.box2')

let btn = document.querySelector('button')

function animate(obj, disdance, speed){

clearInterval(obj.timer)

obj.timer = setInterval(function(){

let moving = obj.offsetLeft

moving += 1

obj.style.left = moving + 'px'

if(moving >disdance){

clearInterval(obj.timer)

}

},speed)

}

animate(box1,300,5)

btn.onclick = function(){

animate(box2,400,3)

}

</script>

</body>

</html>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60