1.判断一排能放下几个
1-1 获取窗口的width
1-2 获取盒子的width
num = window/box
api
width —>content
outerWidth—>content,padding,border
改变css样式
$(element).css({attr:value})**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
</head>
<style>
*{
margin: 0;
padding:0;
}
div.item{
width: 100px;
height: 100px;
border: 1px solid #333;
padding:20px;
float: left;
}
#app{
margin:0 auto;
}
</style>
<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 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"})
console.log(ww)
console.log(box)
console.log(num);
</script>
</body>
</html>
获取元素距离窗口的距离
left: $(element).offset().left;
top: $(element).offset().top;
eq() 找到第几个元素
var w = $(“.item”).offset().left;
var h = $(“.item”).offset().top;
console.log($(“.item”).eq(1));
eq 第几个元素的**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<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>
</body>
</html>
例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></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>
</body>
</html>
判断滚动条是否到达底部
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<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>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<style>
* {
margin: 0;
padding: 0
}
body {
height: 2000px;
background: #ff2d51 url("images/webp.webp") no-repeat;
}
</style>
</head>
<body>
<!--
判断滚动条是否到达底部
-->
<script>
$(window).scroll(function () {
console.log(isReachBottom())
if(isReachBottom()){
/*发送http请求 */
}
})
function isReachBottom() {
var scrollTop = $(window).scrollTop(); //获取滚动条距离顶部的高
var availHeight = $(window).height(); //获取可视区域的高
var dw = $(document).height(); //获取内容区域的高
return (scrollTop + availHeight) > dw -200 ? true : false;
}
</script>
</body>
</html>