1.瀑布流

  1. <script>
  2. http();
  3. $(window).scroll(function () {
  4. console.log(isReachBottom());
  5. if (isReachBottom()) {
  6. http();
  7. }
  8. })
  9. function http() {
  10. var url = "http://192.168.4.18:8000/more"
  11. $.ajax({
  12. url,
  13. type: "get",
  14. success: res => {
  15. console.log(res);
  16. res.forEach(item => {
  17. var html = `
  18. <div class="item"><img src=${item.imageUrl} alt=""></div>
  19. `
  20. $("#app").append(html);
  21. })
  22. setTimeout(Data, 100)
  23. }
  24. })
  25. }
  26. /* 1.得到一排能放几个元素 */
  27. function Data() {
  28. var ww = $(window).width();
  29. var box = $(".item").outerWidth();
  30. var num = Math.floor(ww / box);
  31. /* 2.定义一个数组获取第一排元素的height */
  32. var arr = [];
  33. $(".item").each((index, value) => {
  34. if (index < num) {
  35. console.log(value);
  36. var height = $(value).outerHeight()
  37. console.log(height);
  38. arr.push(height)
  39. }
  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. }
  50. function isReachBottom() {
  51. var scrollTop = $(window).scrollTop(); //获取滚动条距离顶部的距离
  52. var availHeight = $(window).height(); //获取可视区的高度
  53. var dw = $(document).height(); // 获取内容区域的高度
  54. console.log(scrollTop);
  55. console.log(availHeight);
  56. console.log(dw);
  57. var sum = scrollTop + availHeight;
  58. console.log(sum);
  59. return sum > dw - 200 ? true : false;
  60. Data();
  61. }
  62. </script>

2.点击切换

  1. <p>
  2. <a href="#html">html</a>
  3. <a href="#css">css</a>
  4. <a href="#js">javascript</a>
  5. </p>
  6. <div id="html">
  7. html
  8. </div>
  9. <div id="css">
  10. css
  11. </div>
  12. <div id="js">
  13. javascript
  14. </div>
  15. <script>
  16. $("a").click(function(){
  17. var id=$(this).attr("href");
  18. var offsetTop=$(id).offset().top;
  19. console.log(offsetTop);
  20. $("html,body").animate({
  21. scrrollTop:offsetTop
  22. })
  23. return false;
  24. })
  25. </script>

3.下拉刷新

  1. <script>
  2. /* 初始化数据 */
  3. loadData()
  4. jump();
  5. /* 下拉刷新加载的数据 */
  6. $(window).scroll(function () {
  7. if (isReachBottom()) {
  8. var offset = $(".item").length;
  9. loadData(offset);
  10. }
  11. })
  12. /* 自定义传值 */
  13. function jump(){
  14. setTimeout(()=>{
  15. $(".item").click(function(event){
  16. let aid=event.target.dataset.aid;
  17. location.href=`detail.html?id=${aid}`
  18. })
  19. },300)
  20. }
  21. function isReachBottom() {
  22. var scrollTop = $(window).scrollTop();
  23. var availHeight = $(window).height();
  24. var dh = $(document).height();
  25. console.log(scrollTop + availHeight)
  26. return (scrollTop + availHeight == dh) ? true : false;
  27. }
  28. function loadData(offset){
  29. http({
  30. offset,
  31. success: res => {
  32. res.playlists.forEach(item => {
  33. var template = `
  34. <div class="item">
  35. <img src=${item.coverImgUrl}>
  36. </div>
  37. `
  38. $("#app").append(template)
  39. })
  40. }
  41. })
  42. }
  43. </script>