判断滚动条是否到达底部

  1. <style>
  2. *{margin:0;padding:0}
  3. body{
  4. height: 2000px;
  5. background: #ff2d51 url("images/webp.webp") no-repeat;
  6. }
  7. </style>
  8. </head>
  9. <body>
  10. //判断滚动条是否到达底部
  11. <script>
  12. $(window).scroll(function(){
  13. var scrollTop = $(window).scrollTop(); //获取滚动条距离顶部的高
  14. var availHeight = $(window).height(); //获取可视区域的高
  15. var dw = $(document).height(); //获取内容区域的高
  16. console.log(dw)
  17. /* 滚动条的高度+可视区域的height = 内容区域的高度 */
  18. })
  19. function isReachBottom(){
  20. }
  21. </script>

api

  1. <style>
  2. * {
  3. margin: 0;
  4. padding: 0
  5. }
  6. div.item {
  7. width: 200px;
  8. height: 200px;
  9. border: 1px solid #333;
  10. padding: 20px;
  11. float: left;
  12. }
  13. #app{
  14. margin:0 auto;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="app">
  20. <div class="item"></div>
  21. <div class="item"></div>
  22. <div class="item"></div>
  23. <div class="item"></div>
  24. <div class="item"></div>
  25. <div class="item"></div>
  26. </div>
  27. <script>
  28. /*
  29. width -->content
  30. outerWidth-->content,padding,border
  31. */
  32. /*
  33. 改变css样式
  34. $(element).css({attr:value})
  35. */
  36. var ww = $(window).width();
  37. var box = $(".item").outerWidth();
  38. var num = Math.floor(ww / box);
  39. $("#app").css({width:num*box+"px"})
  40. </script>
  41. </body>

获取元素距离窗口的距离

  1. <style>
  2. .item{
  3. width:100px;
  4. height: 100px;
  5. margin:200px;
  6. background-color: red;
  7. }
  8. </style>
  9. </head>
  10. <body>
  11. <!-- 获取元素距离窗口的距离
  12. left: $(element).offset().left;
  13. top: $(element).offset().top;
  14. eq() 找到第几个元素
  15. -->
  16. <div class="item"></div>
  17. <div class="item"></div>
  18. <div class="item"></div>
  19. <script>
  20. // var w = $(".item").offset().left;
  21. // var h = $(".item").offset().top;
  22. console.log($(".item").eq(1));
  23. /* eq 第几个元素的 */
  24. </script>

瀑布流

  1. <style>
  2. *{margin:0;padding:0}
  3. .item img {
  4. width:240px;
  5. padding:5px;
  6. vertical-align: bottom;
  7. }
  8. .item{
  9. float: left;
  10. border:1px solid #eee;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div id="app">
  16. <div class="item"><img src="images/01.jpg" alt=""></div>
  17. <div class="item"><img src="images/02.jpg" alt=""></div>
  18. <div class="item"><img src="images/03.jpg" alt=""></div>
  19. <div class="item"><img src="images/04.jpg" alt=""></div>
  20. <div class="item"><img src="images/05.jpg" alt=""></div>
  21. <div class="item"><img src="images/06.jpg" alt=""></div>
  22. <div class="item"><img src="images/07.jpg" alt=""></div>
  23. <div class="item"><img src="images/08.jpg" alt=""></div>
  24. <div class="item"><img src="images/09.jpg" alt=""></div>
  25. <div class="item"><img src="images/10.jpg" alt=""></div>
  26. <div class="item"><img src="images/11.jpg" alt=""></div>
  27. <div class="item"><img src="images/12.jpg" alt=""></div>
  28. </div>
  29. <script>
  30. /* 1.一排能放置几张图片 */
  31. var ww = $(window).width();
  32. var box = $(".item").outerWidth();
  33. var num = Math.floor(ww/box);
  34. var arr = [] //
  35. /* 2.将第一排的高度放在一个数组中arr */
  36. $(".item").each((index,value)=>{
  37. if(index<num){
  38. var height = $(value).outerHeight();
  39. arr.push(height)
  40. }else{
  41. /* 3.从高度最小的地方放置图片 */
  42. var minHeight = Math.min(...arr);
  43. var index= arr.indexOf(minHeight);
  44. var offsetLeft = $(".item").eq(index).offset().left;
  45. $(value).css({position:"absolute",left:offsetLeft,top:minHeight})
  46. arr[index] = minHeight+ $(value).outerHeight();
  47. }
  48. })
  49. </script>

点击切换load

  1. <div class="head">
  2. <a href="html/html.html">html</a>
  3. <a href="html/css.html">css</a>
  4. </div>
  5. <div id="app">
  6. </div>
  7. <div class="footer">
  8. footer
  9. </div>
  10. <script>
  11. /*
  12. return false 阻止a标签的跳转
  13. */
  14. /*
  15. load()加载服务器上的数据
  16. */
  17. var htmls = ["html/html.html","html/css.html"];
  18. $("#app").load(htmls[0])
  19. $(".head a").click(function(){
  20. $("#app").load(htmls[$(this).index()])
  21. return false;
  22. })
  23. </script>

点击,滚动条滚动到达指定位置

  1. <style>
  2. body{
  3. height: 2500px;
  4. }
  5. .head{
  6. top:0;
  7. left:0;
  8. height: 50px;
  9. position: fixed;
  10. }
  11. div{
  12. width:100px;
  13. height: 100px;
  14. background-color: red;
  15. }
  16. #html,#css,#js{
  17. margin-top: 500px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <p class="head">
  23. <a href="#html">html</a>
  24. <a href="#css">css</a>
  25. <a href="#js">javascript</a>
  26. </p>
  27. <div id="html">
  28. html
  29. </div>
  30. <div id="css">
  31. css
  32. </div>
  33. <div id="js">
  34. javascript
  35. </div>
  36. <script>
  37. /*
  38. $(element).attr(value)
  39. */
  40. $(".head a").click(function(){
  41. var id = $(this).attr("href");
  42. var offsetTop = $(id).offset().top;
  43. console.log(offsetTop)
  44. $("html,body").animate({scrollTop:offsetTop})
  45. return false;
  46. })
  47. </script>

val

  1. <!--
  2. $(element).val() --获取value
  3. $(element).val(value) --改变value
  4. -->
  5. <input type="text" value="你">
  6. <script>
  7. var value = $("input").val("")
  8. console.log(value)
  9. </script>

jQuery转js

  1. var div = $("div")[0]; jqueryjs
  2. var content = div.innerHTML;
  3. console.log(content);

js转jQuery

  1. /* 原生的js对象转换为jquery对象 $(element)*/
  2. var div = document.getElementsByTagName("div")[0];
  3. console.log($(div).html())

下拉刷新

  1. <style>
  2. .item img {
  3. width: 150px;
  4. height: 150px;
  5. padding: 5px;
  6. }
  7. .item {
  8. border: 1px solid #eee;
  9. float: left;
  10. margin-right: 80px;
  11. margin-top: 50px;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="app">
  17. </div>
  18. <script>
  19. /* 初始化数据 */
  20. loadData()
  21. jump();
  22. /* 下拉刷新加载的数据 */
  23. $(window).scroll(function () {
  24. if (isReachBottom()) {
  25. var offset = $(".item").length;
  26. loadData(offset)
  27. }
  28. })
  29. function jump() {
  30. setTimeout(() => {
  31. $(".item").click(function (event) {
  32. let aid = event.currentTarget.dataset.aid;
  33. location.href = `detail.html?id=${aid}`
  34. })
  35. }, 200)
  36. }
  37. function isReachBottom() {
  38. var scrollTop = $(window).scrollTop();
  39. var availHeight = $(window).height();
  40. var dh = $(document).height();
  41. return (scrollTop + availHeight == dh) ? true : false;
  42. }
  43. function loadData(offset) {
  44. http({
  45. offset,
  46. success: res => {
  47. var playlists = res.playlists;
  48. playlists.forEach(item => {
  49. var template = `
  50. <div class="item" data-aid=${item.id}>
  51. <img src=${item.coverImgUrl}>
  52. </div>
  53. `
  54. $("#app").append(template)
  55. })
  56. }
  57. })
  58. }
  59. </script>