判断滚动条是否到达底部
<style> *{margin:0;padding:0} body{ height: 2000px; background: #ff2d51 url("images/webp.webp") no-repeat; } </style></head><body> //判断滚动条是否到达底部 <script> $(window).scroll(function(){ var scrollTop = $(window).scrollTop(); //获取滚动条距离顶部的高 var availHeight = $(window).height(); //获取可视区域的高 var dw = $(document).height(); //获取内容区域的高 console.log(dw) /* 滚动条的高度+可视区域的height = 内容区域的高度 */ }) function isReachBottom(){ } </script>
api
<style> * { margin: 0; padding: 0 } div.item { width: 200px; height: 200px; border: 1px solid #333; padding: 20px; float: left; } #app{ margin:0 auto; } </style></head><body> <div id="app"> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> </div> <script> /* width -->content outerWidth-->content,padding,border */ /* 改变css样式 $(element).css({attr:value}) */ var ww = $(window).width(); var box = $(".item").outerWidth(); var num = Math.floor(ww / box); $("#app").css({width:num*box+"px"}) </script></body>
获取元素距离窗口的距离
<style> .item{ width:100px; height: 100px; margin:200px; background-color: red; } </style></head><body> <!-- 获取元素距离窗口的距离 left: $(element).offset().left; top: $(element).offset().top; eq() 找到第几个元素 --> <div class="item"></div> <div class="item"></div> <div class="item"></div> <script> // var w = $(".item").offset().left; // var h = $(".item").offset().top; console.log($(".item").eq(1)); /* eq 第几个元素的 */ </script>
瀑布流
<style> *{margin:0;padding:0} .item img { width:240px; padding:5px; vertical-align: bottom; } .item{ float: left; border:1px solid #eee; } </style></head><body> <div id="app"> <div class="item"><img src="images/01.jpg" alt=""></div> <div class="item"><img src="images/02.jpg" alt=""></div> <div class="item"><img src="images/03.jpg" alt=""></div> <div class="item"><img src="images/04.jpg" alt=""></div> <div class="item"><img src="images/05.jpg" alt=""></div> <div class="item"><img src="images/06.jpg" alt=""></div> <div class="item"><img src="images/07.jpg" alt=""></div> <div class="item"><img src="images/08.jpg" alt=""></div> <div class="item"><img src="images/09.jpg" alt=""></div> <div class="item"><img src="images/10.jpg" alt=""></div> <div class="item"><img src="images/11.jpg" alt=""></div> <div class="item"><img src="images/12.jpg" alt=""></div> </div> <script> /* 1.一排能放置几张图片 */ var ww = $(window).width(); var box = $(".item").outerWidth(); var num = Math.floor(ww/box); var arr = [] // /* 2.将第一排的高度放在一个数组中arr */ $(".item").each((index,value)=>{ if(index<num){ var height = $(value).outerHeight(); arr.push(height) }else{ /* 3.从高度最小的地方放置图片 */ var minHeight = Math.min(...arr); var index= arr.indexOf(minHeight); var offsetLeft = $(".item").eq(index).offset().left; $(value).css({position:"absolute",left:offsetLeft,top:minHeight}) arr[index] = minHeight+ $(value).outerHeight(); } }) </script>
点击切换load
<div class="head"> <a href="html/html.html">html</a> <a href="html/css.html">css</a> </div> <div id="app"> </div> <div class="footer"> footer </div> <script> /* return false 阻止a标签的跳转 */ /* load()加载服务器上的数据 */ var htmls = ["html/html.html","html/css.html"]; $("#app").load(htmls[0]) $(".head a").click(function(){ $("#app").load(htmls[$(this).index()]) return false; }) </script>
点击,滚动条滚动到达指定位置
<style> body{ height: 2500px; } .head{ top:0; left:0; height: 50px; position: fixed; } div{ width:100px; height: 100px; background-color: red; } #html,#css,#js{ margin-top: 500px; } </style></head><body> <p class="head"> <a href="#html">html</a> <a href="#css">css</a> <a href="#js">javascript</a> </p> <div id="html"> html </div> <div id="css"> css </div> <div id="js"> javascript </div> <script> /* $(element).attr(value) */ $(".head a").click(function(){ var id = $(this).attr("href"); var offsetTop = $(id).offset().top; console.log(offsetTop) $("html,body").animate({scrollTop:offsetTop}) return false; }) </script>
val
<!-- $(element).val() --获取value值 $(element).val(value) --改变value值 --> <input type="text" value="你"> <script> var value = $("input").val("") console.log(value) </script>
jQuery转js
var div = $("div")[0]; jquery转js var content = div.innerHTML; console.log(content);
js转jQuery
/* 原生的js对象转换为jquery对象 $(element)*/ var div = document.getElementsByTagName("div")[0]; console.log($(div).html())
下拉刷新
<style> .item img { width: 150px; height: 150px; padding: 5px; } .item { border: 1px solid #eee; float: left; margin-right: 80px; margin-top: 50px; } </style></head><body> <div id="app"> </div> <script> /* 初始化数据 */ loadData() jump(); /* 下拉刷新加载的数据 */ $(window).scroll(function () { if (isReachBottom()) { var offset = $(".item").length; loadData(offset) } }) function jump() { setTimeout(() => { $(".item").click(function (event) { let aid = event.currentTarget.dataset.aid; location.href = `detail.html?id=${aid}` }) }, 200) } function isReachBottom() { var scrollTop = $(window).scrollTop(); var availHeight = $(window).height(); var dh = $(document).height(); return (scrollTop + availHeight == dh) ? true : false; } function loadData(offset) { http({ offset, success: res => { var playlists = res.playlists; playlists.forEach(item => { var template = ` <div class="item" data-aid=${item.id}> <img src=${item.coverImgUrl}> </div> ` $("#app").append(template) }) } }) } </script>