经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
前端之jquery1
来源:cnblogs  作者:YifChan  时间:2019/6/10 13:40:04  对本文有异议

jquery介绍

jQuery是目前使用最广泛的javascript函数库。据统计,全世界排名前100万的网站,有46%使用jQuery,远远超过其他库。微软公司甚至把jQuery作为他们的官方库。

jQuery的版本分为1.x系列和2.x、3.x系列,1.x系列兼容低版本的浏览器,2.x、3.x系列放弃支持低版本浏览器,目前使用最多的是1.x系列的。

jquery是一个函数库,一个js文件,页面用script标签引入这个js文件就可以使用。

<script type="text/javascript" src="js/jquery-1.12.2.js"></script>
jquery的口号和愿望 Write Less, Do More(写得少,做得多)

1、http://jquery.com/ 官方网站
2、https://code.jquery.com/ 版本下载

jquery加载

将获取元素的语句写到页面头部,会因为元素还没有加载而出错,jquery提供了ready方法解决这个问题,它的速度比原生的 window.onload 更快。

  1. <script type="text/javascript">
  2. $(document).ready(function(){
  3. ......
  4. });
  5. </script>

可以简写为:

  1. <script type="text/javascript">
  2. $(function(){
  3. ......
  4. });
  5. </script>

 

jquery加载示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  7. <script type="text/javascript">
  8. window.onload = function () {
  9. var oDiv = document.getElementById('div1');
  10. alert('原生弹出的' + oDiv);
  11. };
  12. /*
  13. // ready 比window.onload 要快:
  14. //原因是,window.onload是等标签加载完后,再渲染完之后才运行,
  15. // ready是等标签加载完后就运行
  16. // ready的完整写法:
  17. $(document).ready(function(){
  18. var $div = $('#div1');
  19. alert('jquery弹出的'+$div);
  20. })
  21. */
  22.  
  23. // ready的简写:
  24. $(function () {
  25. var $div = $('#div1');
  26. alert('jquery弹出的' + $div);
  27. })
  28. </script>
  29. </head>
  30. <body>
  31. <div id="div1">这是一个div元素</div>
  32. </body>
  33. </html>
jquery加载示例

 

jquery选择器

jquery用法思想一

选择某个网页元素,然后对它进行某种操作

jquery选择器

jquery选择器可以快速地选择元素,选择规则和css样式相同,使用length属性判断是否选择成功。

  1. $(document) //选择整个文档对象
  2. $('li') //选择所有的li元素
  3. $('#myId') //选择id为myId的网页元素
  4. $('.myClass') // 选择class为myClass的元素
  5. $('input[name=first]') // 选择name属性等于first的input元素
  6. $('#ul1 li span') //选择id为为ul1元素下的所有li下的span元素

对选择集进行修饰过滤(类似CSS伪类)

  1. $('#ul1 li:first') //选择id为ul1元素下的第一个li
  2. $('#ul1 li:odd') //选择id为ul1元素下的li的奇数行
  3. $('#ul1 li:eq(2)') //选择id为ul1元素下的第3个li
  4. $('#ul1 li:gt(2)') // 选择id为ul1元素下的前三个之后的li
  5. $('#myForm :input') // 选择表单中的input元素
  6. $('div:visible') //选择可见的div元素

对选择集进行函数过滤

  1. $('div').has('p'); // 选择包含p元素的div元素
  2. $('div').not('.myClass'); //选择class不等于myClass的div元素
  3. $('div').filter('.myClass'); //选择class等于myClass的div元素
  4. $('div').first(); //选择第1个div元素
  5. $('div').eq(5); //选择第6个div元素

选择集转移

  1. $('div').prev('p'); //选择div元素前面的第一个p元素
  2. $('div').next('p'); //选择div元素后面的第一个p元素
  3. $('div').closest('form'); //选择离div最近的那个form父元素
  4. $('div').parent(); //选择div的父元素
  5. $('div').children(); //选择div的所有子元素
  6. $('div').siblings(); //选择div的同级元素
  7. $('div').find('.myClass'); //选择div内的class等于myClass的元素

 

jquery选择器示例1

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  7. <script type="text/javascript">
  8. $(function () {
  9. var $div = $('#div1');
  10. $div.css({'color': 'red'});
  11. var $div2 = $('.box');
  12. $div2.css({'color': 'green'});
  13. var $li = $('.list li');
  14. // 带“-”的样式属性,可以写成驼峰式,也可以写成原始
  15. $li.css({'background-color': 'pink', 'color': 'red'});
  16. });
  17. </script>
  18. </head>
  19.  
  20. <body>
  21. <div id="div1">这是一个div元素</div>
  22. <div class="box">这是第二个div元素</div>
  23. <div class="box">这是第三个div元素</div>
  24.  
  25. <!-- ul.list>li{$}*8 -->
  26. <ul class="list">
  27. <li>1</li>
  28. <li>2</li>
  29. <li>3</li>
  30. <li>4</li>
  31. <li>5</li>
  32. <li>6</li>
  33. <li>7</li>
  34. <li>8</li>
  35. </ul>
  36.  
  37. </body>
  38. </html>
jquery选择器示例1

选择器过滤和转移示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  7. <script type="text/javascript">
  8. $(function () {
  9. /*
  10. var $div = $('div');
  11. $div.css({'backgroundColor':'gold'});
  12. */
  13. $('div').css({'backgroundColor': 'gold'});
  14. $('div').has('p').css({'fontSize': '30px'});
  15. $('div').eq(4).css({'textIndent': '20px'});
  16. $('div').eq(4).prev().css({'color': 'red'});
  17. $('div').find('.tip').css({'fontSize': '30px'});
  18. })
  19. </script>
  20. </head>
  21. <body>
  22. <div>1</div>
  23. <div><p>2</p></div>
  24. <div>3</div>
  25. <div>4</div>
  26. <div>5</div>
  27. <div>6</div>
  28. <div>7</div>
  29. <div><span>8</span><span class="tip">9</span></div>
  30. </body>
  31. </html>
选择器过滤和转移示例

判断是否选中元素示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  7. <script type="text/javascript">
  8. $(function () {
  9. $div = $('#div1');
  10. alert($div.length);
  11. // 没有选中元素,也不会报错,程序正常运行
  12. $div2 = $('#div2');
  13. alert($div2.length);
  14. // if($div2.length>0) 通过length属性来判断是否选中了元素
  15. })
  16. </script>
  17. </head>
  18. <body>
  19. <div id="div1">div元素</div>
  20.  
  21. </body>
  22. </html>
判断是否选中元素示例

 

jquery样式操作

jquery用法思想二
同一个函数完成取值和赋值

操作行间样式

  1. // 获取div的样式
  2. $("div").css("width");
  3. $("div").css("color");
  4. //设置div的样式
  5. $("div").css("width","30px");
  6. $("div").css("height","30px");
  7. $("div").css({fontSize:"30px",color:"red"});

特别注意
选择器获取的多个元素,获取信息获取的是第一个,比如:$("div").css("width"),获取的是第一个div的width。

操作样式类名

  1. $("#div1").addClass("divClass2") //为id为div1的对象追加样式divClass2
  2. $("#div1").removeClass("divClass") //移除id为div1的对象的class名为divClass的样式
  3. $("#div1").removeClass("divClass divClass2") //移除多个样式
  4. $("#div1").toggleClass("anotherClass") //重复切换anotherClass样式,有就删除,没有就添加

 

css方法示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  7. <script type="text/javascript">
  8. $(function () {
  9. var $div = $('#box');
  10. // 读取属性值
  11. var sTr = $div.css('fontSize');
  12. alert(sTr);
  13. // 写属性值
  14. $div.css({'color': 'red', 'backgroundColor': 'pink', 'fontSize': '20px'});
  15. /*
  16. 原生js无法读取行间没有定义的css属性值
  17. var oDiv = document.getElementById('box');
  18. alert(oDiv.style.fontSize);
  19. */
  20. })
  21. </script>
  22. </head>
  23. <body>
  24. <div id="box">div元素</div>
  25. </body>
  26. </html>
css方法示例

样式名操作示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  7. <script type="text/javascript">
  8. $(function () {
  9. var $div = $('.box');
  10. // 在原来样式名的基础上加上“big”的样式名
  11. $div.addClass('big');
  12. // 在原来样式名的基础上去掉“red”的样式名
  13. $div.removeClass('red');
  14. })
  15. </script>
  16. <style type="text/css">
  17. .box {
  18. width: 100px;
  19. height: 100px;
  20. background-color: gold;
  21. }
  22. .big {
  23. font-size: 30px;
  24. }
  25. .red {
  26. color: red;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div class="box red">div元素</div>
  32. </body>
  33. </html>
样式名操作示例

 

绑定click事件

给元素绑定click事件,可以用如下方法:

  1. $('#btn1').click(function(){
  2. // 内部的this指的是原生对象
  3. // 使用jquery对象用 $(this)
  4. })

 

绑定click事件-toggleclass使用示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  7. <script type="text/javascript">
  8. $(function () {
  9. // 绑定click事件
  10. $('#btn').click(function () {
  11. // if ($('.box').hasClass('col01')) {
  12. // $('.box').removeClass('col01');
  13. // } else {
  14. // $('.box').addClass('col01');
  15. // }
  16. //
  17. // // 简化成下面的写法:
  18. $('.box').toggleClass('col01');
  19. })
  20. })
  21. </script>
  22. </head>
  23. <style type="text/css">
  24. .box {
  25. width: 200px;
  26. height: 200px;
  27. background-color: gold;
  28. }
  29. .col01 {
  30. background-color: green;
  31. }
  32. </style>
  33. <body>
  34. <input type="button" name="" value="切换样式" id="btn">
  35. <div class="box">div元素</div>
  36. </body>
  37. </html>
绑定click事件-toggleclass使用示例

 

索引值-选项卡

获取元素的索引值
有时候需要获得匹配元素相对于其同胞元素的索引位置,此事时可以用index()方法进行获取;

获取元素索引值示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  7. <script type="text/javascript">
  8. $(function () {
  9. var $li = $('.list li');
  10. //alert($li.length); 弹出 8
  11. //alert($li.eq(3).index()); 弹出3
  12. alert($li.filter('.myli').index());
  13. })
  14. </script>
  15. </head>
  16. <body>
  17. <ul class="list">
  18. <li>1</li>
  19. <li>2</li>
  20. <li>3</li>
  21. <li>4</li>
  22. <li class="myli">5</li>
  23. <li>6</li>
  24. <li>7</li>
  25. <li>8</li>
  26. </ul>
  27. </body>
  28. </html>
获取元素索引值示例

选项卡示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style type="text/css">
  7. .btns input {
  8. width: 100px;
  9. height: 40px;
  10. background-color: #ddd;
  11. border: 0;
  12. }
  13. .btns .current {
  14. background-color: gold;
  15. }
  16. .cons div {
  17. width: 500px;
  18. height: 300px;
  19. background-color: gold;
  20. display: none;
  21. text-align: center;
  22. line-height: 300px;
  23. font-size: 30px;
  24. }
  25. .cons .active {
  26. display: block;
  27. }
  28. </style>
  29. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  30. <script type="text/javascript">
  31. $(function () {
  32. var $btn = $('.btns input');
  33. var $div = $('.cons div');
  34. $btn.click(function () {
  35. // this 指的是原生的this,它表示当前点击的对象,使用jquery的对象需要用$(this)
  36.  
  37. // 当前点击的按钮加上current样式后,除了当前,其他的按钮去掉current样式
  38. $(this).addClass('current').siblings().removeClass('current');
  39. //alert( $(this).index() ; //弹出当前按钮的索引值
  40. // 当前点击的按钮对应索引值的div加上active样式,其他的去掉active样式
  41. $div.eq($(this).index()).addClass('active').siblings().removeClass('active');
  42. })
  43. })
  44. </script>
  45. </head>
  46. <body>
  47. <div class="btns">
  48. <input type="button" name="" value="01" class="current">
  49. <input type="button" name="" value="02">
  50. <input type="button" name="" value="03">
  51. </div>
  52. <div class="cons">
  53. <div class="active">选项卡一的内容</div>
  54. <div>选项卡二的内容</div>
  55. <div>选项卡三的内容</div>
  56. </div>
  57. </body>
  58. </html>
选项卡示例

 

 

jquery特殊效果

  1. fadeIn() 淡入
  2. $btn.click(function(){
  3. $('#div1').fadeIn(1000,'swing',function(){
  4. alert('done!');
  5. });
  6. });
  7. fadeOut() 淡出
  8. fadeToggle() 切换淡入淡出
  9. hide() 隐藏元素
  10. show() 显示元素
  11. toggle() 依次展示或隐藏某个元素
  12. slideDown() 向下展开
  13. slideUp() 向上卷起
  14. slideToggle() 依次展开或卷起某个元素

 

jquery特殊效果示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style type="text/css">
  7. .box {
  8. width: 300px;
  9. height: 300px;
  10. background-color: gold;
  11. display: none;
  12. }
  13. </style>
  14. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  15. <script type="text/javascript">
  16. $(function () {
  17. $('#btn').click(function () {
  18. /*$('.box').fadeIn(1000,function(){
  19. alert('动画完了!');
  20. });
  21. */
  22.  
  23. /*$('.box').fadeToggle(1000,function(){
  24. alert('动画完了!');
  25. });
  26. */
  27.  
  28. // $('.box').show();
  29.  
  30. //$('.box').toggle();
  31.  
  32. //$('.box').slideDown();
  33. $('.box').slideToggle(1000, function () {
  34. alert('动画完了')
  35. });
  36. })
  37. })
  38. </script>
  39. </head>
  40.  
  41. <body>
  42. <input type="button" name="" value="动画" id="btn">
  43. <div class="box"></div>
  44. </body>
  45. </html>
jquery特殊效果示例

 

jquery链式调用

jquery对象的方法会在执行完后返回这个jquery对象,所有jquery对象的方法可以连起来写:

  1. $('#div1') // id为div1的元素
  2. .children('ul') //该元素下面的ul子元素
  3. .slideDown('fast') //高度从零变到实际高度来显示ul元素
  4. .parent() //跳到ul的父元素,也就是id为div1的元素
  5. .siblings() //跳到div1元素平级的所有兄弟元素
  6. .children('ul') //这些兄弟元素中的ul子元素
  7. .slideUp('fast'); //高度实际高度变换到零来隐藏ul元素

 

链式调用-层级菜单示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>层级菜单</title>
  6. <style type="text/css">
  7. body {
  8. font-family: 'Microsoft Yahei';
  9. }
  10. body, ul {
  11. margin: 0px;
  12. padding: 0px;
  13. }
  14. ul {
  15. list-style: none;
  16. }
  17. .menu {
  18. width: 200px;
  19. margin: 20px auto 0;
  20. }
  21. .menu .level1, .menu li ul a {
  22. display: block;
  23. width: 200px;
  24. height: 30px;
  25. line-height: 30px;
  26. text-decoration: none;
  27. background-color: #3366cc;
  28. color: #fff;
  29. font-size: 16px;
  30. text-indent: 10px;
  31. }
  32. .menu .level1 {
  33. border-bottom: 1px solid #afc6f6;
  34. }
  35. .menu li ul a {
  36. font-size: 14px;
  37. text-indent: 20px;
  38. background-color: #7aa1ef;
  39. }
  40. .menu li ul li {
  41. border-bottom: 1px solid #afc6f6;
  42. }
  43. .menu li ul {
  44. display: none;
  45. }
  46. .menu li ul.current {
  47. display: block;
  48. }
  49. .menu li ul li a:hover {
  50. background-color: #f6b544;
  51. }
  52. </style>
  53.  
  54. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  55. <script type="text/javascript">
  56. $(function () {
  57. $('.level1').click(function () {
  58. //当前点击的元素紧挨的同辈元素向下展开,再跳到此元素的父级(li),再跳到此父级的其他的同辈元素(li),
  59. // 选择其他同辈元素(li)的子元素ul,然后将它向上收起。
  60. // 通过stop() 可以修正反复点击导致的持续动画的问题
  61. $(this).next().stop().slideToggle().parent().siblings().children('ul').slideUp();
  62. })
  63. })
  64. </script>
  65. </head>
  66. <body>
  67. <ul class="menu">
  68. <li>
  69. <a href="#" class="level1">水果</a>
  70. <ul class="current">
  71. <li><a href="#">苹果</a></li>
  72. <li><a href="#">梨子</a></li>
  73. <li><a href="#">葡萄</a></li>
  74. <li><a href="#">火龙果</a></li>
  75. </ul>
  76. </li>
  77. <li>
  78. <a href="#" class="level1">海鲜</a>
  79. <ul>
  80. <li><a href="#">蛏子</a></li>
  81. <li><a href="#">扇贝</a></li>
  82. <li><a href="#">龙虾</a></li>
  83. <li><a href="#">象拔蚌</a></li>
  84. </ul>
  85. </li>
  86. <li>
  87. <a href="#" class="level1">肉类</a>
  88. <ul>
  89. <li><a href="#">内蒙古羊肉</a></li>
  90. <li><a href="#">进口牛肉</a></li>
  91. <li><a href="#">野猪肉</a></li>
  92. </ul>
  93. </li>
  94. <li>
  95. <a href="#" class="level1">蔬菜</a>
  96. <ul>
  97. <li><a href="#">娃娃菜</a></li>
  98. <li><a href="#">西红柿</a></li>
  99. <li><a href="#">西芹</a></li>
  100. <li><a href="#">胡萝卜</a></li>
  101. </ul>
  102. </li>
  103. <li>
  104. <a href="#" class="level1">速冻</a>
  105. <ul>
  106. <li><a href="#">冰淇淋</a></li>
  107. <li><a href="#">湾仔码头</a></li>
  108. <li><a href="#">海参</a></li>
  109. <li><a href="#">牛肉丸</a></li>
  110. </ul>
  111. </li>
  112. </ul>
  113. </body>
  114. </html>
链式调用-层级菜单示例

 

 

 

jquery动画

通过animate方法可以设置元素某属性值上的动画,可以设置一个或多个属性值,动画执行完成后会执行一个函数。

  1. $('#div1').animate({
  2. width:300,
  3. height:300
  4. },1000,swing,function(){
  5. alert('done!');
  6. });

参数可以写成数字表达式:

  1. $('#div1').animate({
  2. width:'+=100',
  3. height:300
  4. },1000,swing,function(){
  5. alert('done!');
  6. });

 

animate动画实例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  7. <script type="text/javascript">
  8. $(function () {
  9. $('#btn').click(function () {
  10. $('.box').animate({'width': 600}, 1000, function () {
  11. $('.box').animate({'height': 400}, 1000, function () {
  12. $('.box').animate({'opacity': 0});
  13. });
  14. });
  15. });
  16. $('#btn2').click(function () {
  17. $('.box2').stop().animate({'width': '+=100'});
  18. })
  19. })
  20. </script>
  21. <style type="text/css">
  22. .box, .box2 {
  23. width: 100px;
  24. height: 100px;
  25. background-color: gold;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <input type="button" name="" value="动画" id="btn">
  31. <div class="box"></div>
  32.  
  33. <br/>
  34. <br/>
  35. <input type="button" name="" value="动画" id="btn2">
  36. <div class="box2"></div>
  37. </body>
  38. </html>
animate动画实例

滑动选项卡

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style type="text/css">
  7. .btns input {
  8. width: 100px;
  9. height: 40px;
  10. background-color: #ddd;
  11. border: 0;
  12. outline: none;
  13. }
  14. .btns .current {
  15. background-color: gold;
  16. }
  17. .cons {
  18. width: 500px;
  19. height: 300px;
  20. overflow: hidden;
  21. position: relative;
  22. }
  23. .slides {
  24. width: 1500px;
  25. height: 300px;
  26. position: absolute;
  27. left: 0;
  28. top: 0;
  29. }
  30. .cons .slides div {
  31. width: 500px;
  32. height: 300px;
  33. background-color: gold;
  34. text-align: center;
  35. line-height: 300px;
  36. font-size: 30px;
  37. float: left;
  38. }
  39. .cons .active {
  40. display: block;
  41. }
  42. </style>
  43. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  44. <script type="text/javascript">
  45. $(function () {
  46. var $btn = $('.btns input');
  47. var $slides = $('.cons .slides');
  48. $btn.click(function () {
  49. // this 指的是原生的this,它表示当前点击的对象,使用jquery的对象需要用$(this)
  50.  
  51. // 当前点击的按钮加上current样式后,除了当前,其他的按钮去掉current样式
  52. $(this).addClass('current').siblings().removeClass('current');
  53. $slides.stop().animate({'left': -500 * $(this).index()});
  54. })
  55. })
  56. </script>
  57. </head>
  58. <body>
  59. <div class="btns">
  60. <input type="button" name="" value="01" class="current">
  61. <input type="button" name="" value="02">
  62. <input type="button" name="" value="03">
  63. </div>
  64. <div class="cons">
  65. <div class="slides">
  66. <div>选项卡一的内容</div>
  67. <div>选项卡二的内容</div>
  68. <div>选项卡三的内容</div>
  69. </div>
  70. </div>
  71. </body>
  72. </html>
滑动选项卡

 

 

元素的尺寸、位置和页面滚动事件

1、获取和设置元素的尺寸

  1. width()、height() 获取元素widthheight
  2. innerWidth()、innerHeight() 包括paddingwidthheight
  3. outerWidth()、outerHeight() 包括paddingborderwidthheight
  4. outerWidth(true)、outerHeight(true) 包括paddingborder以及marginwidthheight

2、获取元素相对页面的绝对位置

  1. offse()

3、获取可视区高度

  1. $(window).height();

4、获取页面高度

  1. $(document).height();

5、获取页面滚动距离

  1. $(document).scrollTop();
  2. $(document).scrollLeft();

6、页面滚动事件

  1. $(window).scroll(function(){
  2. ......
  3. })

 

获取元素尺寸示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  7. <script type="text/javascript">
  8. $(function () {
  9. var $div = $('.box');
  10. // 盒子内容的尺寸
  11. console.log($div.width());
  12. console.log($div.height());
  13. // 盒子内容加上padding的尺寸
  14. console.log($div.innerWidth());
  15. console.log($div.innerHeight());
  16. //盒子的真实尺寸,内容尺寸+padding+border
  17. console.log($div.outerWidth());
  18. console.log($div.outerHeight());
  19. // 盒子的真实尺寸再加上margin
  20. console.log($div.outerWidth(true));
  21. console.log($div.outerHeight(true));
  22. })
  23. </script>
  24. <style type="text/css">
  25. .box {
  26. width: 300px;
  27. height: 200px;
  28. padding: 20px;
  29. border: 10px solid #000;
  30. margin: 20px;
  31. background-color: gold;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <div class="box"></div>
  37. </body>
  38. </html>
获取元素尺寸示例

获取元素绝对位置示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  7. <script type="text/javascript">
  8. $(function () {
  9. var $div = $('.box');
  10. $div.click(function () {
  11. var oPos = $div.offset();
  12. document.title = oPos.left + "|" + oPos.top;
  13. })
  14. //console.log(oPos);
  15. })
  16. </script>
  17. </head>
  18. <style type="text/css">
  19. .box {
  20. width: 200px;
  21. height: 200px;
  22. background-color: gold;
  23. margin: 50px auto 0;
  24. }
  25. </style>
  26. <body>
  27. <div class="box">
  28.  
  29. </div>
  30. </body>
  31. </html>
获取元素绝对位置示例

加入购物车动画示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style type="text/css">
  7. .chart {
  8. width: 150px;
  9. height: 50px;
  10. border: 2px solid #000;
  11. text-align: center;
  12. line-height: 50px;
  13. float: right;
  14. margin-right: 100px;
  15. margin-top: 50px;
  16. }
  17. .chart em {
  18. font-style: normal;
  19. color: red;
  20. font-weight: bold;
  21. }
  22. .add {
  23. width: 100px;
  24. height: 50px;
  25. background-color: green;
  26. border: 0;
  27. color: #fff;
  28. float: left;
  29. margin-top: 300px;
  30. margin-left: 300px;
  31. }
  32. .point {
  33. width: 16px;
  34. height: 16px;
  35. background-color: red;
  36. position: fixed;
  37. left: 0;
  38. top: 0;
  39. display: none;
  40. z-index: 9999;
  41. border-radius: 50%;
  42. }
  43.  
  44. </style>
  45. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  46. <script type="text/javascript">
  47. $(function () {
  48. var $chart = $('.chart');
  49. var $count = $('.chart em');
  50. var $btn = $('.add');
  51. var $point = $('.point');
  52. var $w01 = $btn.outerWidth();
  53. var $h01 = $btn.outerHeight();
  54. var $w02 = $chart.outerWidth();
  55. var $h02 = $chart.outerHeight();
  56. $btn.click(function () {
  57. var oPos01 = $btn.offset();
  58. var oPos02 = $chart.offset();
  59. $point.css({'left': oPos01.left + parseInt($w01 / 2) - 8, 'top': oPos01.top + parseInt($h01 / 2) - 8});
  60. $point.show();
  61. $point.stop().animate({
  62. 'left': oPos02.left + parseInt($w02 / 2) - 8,
  63. 'top': oPos02.top + parseInt($h02 / 2) - 8
  64. }, 800, function () {
  65. $point.hide();
  66. var iNum = $count.html();
  67. $count.html(parseInt(iNum) + 1);
  68. });
  69. })
  70. });
  71. </script>
  72. </head>
  73. <body>
  74. <div class="chart">购物车<em>0</em></div>
  75. <input type="button" name="" value="加入购物车" class="add">
  76. <div class="point"></div>
  77. </body>
  78. </html>
加入购物车动画

scrollleft-top-悬浮菜单的使用
可视区尺寸-文档尺寸示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  7. <script type="text/javascript">
  8. $(function(){
  9. console.log('可视区的宽度:'+$(window).width() );
  10. console.log('可视区的高度:'+$(window).height() );
  11. console.log('文档的宽度'+$(document).width() );
  12. console.log('文档的高度:'+$(document).height() );
  13. })
  14. </script>
  15. </head>
  16. <body>
  17. <p>文档内容</p>
  18. <br />
  19. <br />
  20. <br />
  21. <br />
  22. <br />
  23. <br />
  24. <br />
  25. <br />
  26. <br />
  27. <br />
  28. <p>文档内容</p>
  29. ...
  30. <p>文档内容</p>
  31. </body>
  32. </html>
可视区尺寸-文档尺寸示例

置顶菜单示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style type="text/css">
  7. body {
  8. margin: 0;
  9. }
  10. .banner {
  11. width: 960px;
  12. height: 200px;
  13. background-color: cyan;
  14. margin: 0 auto;
  15. }
  16. .menu {
  17. width: 960px;
  18. height: 80px;
  19. background-color: gold;
  20. margin: 0 auto;
  21. text-align: center;
  22. line-height: 80px;
  23. }
  24. .menu_back {
  25. width: 960px;
  26. height: 80px;
  27. margin: 0 auto;
  28. display: none;
  29. }
  30. p {
  31. text-align: center;
  32. color: red;
  33. }
  34. .totop {
  35. width: 60px;
  36. height: 60px;
  37. background-color: #000;
  38. color: #fff;
  39. position: fixed;
  40. right: 20px;
  41. bottom: 50px;
  42. line-height: 60px;
  43. text-align: center;
  44. font-size: 40px;
  45. border-radius: 50%;
  46. cursor: pointer;
  47. display: none;
  48. }
  49. </style>
  50. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  51. <script type="text/javascript">
  52. $(function () {
  53. $menu = $('.menu');
  54. $menu_back = $('.menu_back');
  55. $totop = $('.totop');
  56. $(window).scroll(function () {
  57. //console.log('abc');
  58. var iNum = $(document).scrollTop();
  59. //document.title = iNum;
  60.  
  61. if (iNum > 200) {
  62. $menu.css({
  63. 'position': 'fixed',
  64. 'left': '50%',
  65. 'top': 0,
  66. 'marginLeft': -480
  67. });
  68. $menu_back.show();
  69. } else {
  70. $menu.css({
  71. 'position': 'static',
  72. 'marginLeft': 'auto'
  73. });
  74. $menu_back.hide();
  75. }
  76. if (iNum > 400) {
  77. $totop.fadeIn();
  78. } else {
  79. $totop.fadeOut();
  80. }
  81. });
  82. $totop.click(function () {
  83. $('html,body').animate({'scrollTop': 0});
  84. })
  85. })
  86. </script>
  87. </head>
  88. <body>
  89. <div class="banner"></div>
  90. <div class="menu">菜单</div>
  91. <div class="menu_back"></div>
  92. <div class="totop"></div>
  93. <p>文档内容</p>
  94. <br/>
  95. <br/>
  96. <br/>
  97. <br/>
  98. <br/>
  99. <br/>
  100. <br/>
  101. <br/>
  102. <br/>
  103. <br/>
  104. <p>文档内容</p>
  105. ...
  106. <p>文档内容</p>
  107. </body>
  108. </html>
置顶菜单示例

原文链接:http://www.cnblogs.com/yifchan/p/html-1-9.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号