<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 500px;
height: 400px;
}
.tap {
width: 500px;
height: 50px;
border: 1px solid green;
}
ul {
margin: 13px auto;
padding-left: 0;
list-style: none;
width: 400px;
display: flex;
justify-content: space-between;
}
li {
width: 80px;
background-color: white;
text-align: center;
color: black;
cursor: pointer;
}
.add {
background-color: green;
color: white;
}
.content {
margin-top: 1px;
width: 500px;
height: 350px;
/* border: 1px solid red; */
}
.content1 {
display: none;
}
.no {
display: block;
}
.box1 {
width: 480px;
height: 320px;
margin-top: 10px;
margin-left: 10px;
/* border: 1px solid orange; */
display: flex;
/* justify-content: space-between; */
/* align-content: space-between; */
flex-wrap: wrap;
}
.box2 {
width: 120px;
height: 150px;
/* border: 1px solid green; */
text-align: center;
}
img {
width: 100px;
height: 100px;
}
p {
margin-top: 0;
}
</style>
</head>
<body>
<div class="box">
<div class="tap">
<ul>
<li class="add filter" data-id="0">0-100元</li>
<li data-id="1">100-300元</li>
<li data-id="2">300元以上</li>
<li data-id="3">全部区间</li>
</ul>
</div>
<div class="content">
<div class="content1 no" >
<div class="box1">
<!-- <div class="box2">
<img src="../DOM/image/img10.jpg" alt="">
<span>文字1</span>
<p>¥400</p>
</div> -->
</div>
</div>
<div class="content1">
<div class="box1">
</div>
</div>
<div class="content1">
<div class="box1">
</div>
</div>
<div class="content1">
<div class="box1">
</div>
</div>
</div>
</div>
<script>
const ul = document.querySelector("ul")
const li = document.querySelectorAll('li')
const content1 = document.querySelectorAll('.content1');
const box1 = document.querySelectorAll('.box1');
const obj = [
{url: '../DOM/image/img10.jpg',text: '文字1',price: 90},
{url: '../DOM/image/img11.jpg',text: '文字1',price: 120},
{url: '../DOM/image/img12.jpg',text: '文字1',price: 130},
{url: '../DOM/image/img13.jpg',text: '文字1',price: 140},
{url: '../DOM/image/img14.jpg',text: '文字1',price: 400},
{url: '../DOM/image/img10.jpg',text: '文字1',price: 450},
{url: '../DOM/image/img11.jpg',text: '文字1',price: 460},
{url: '../DOM/image/img12.jpg',text: '文字1',price: 470},
]
ul.addEventListener('click',e => {
li.forEach((ele,index) => {
if (e.target.tagName === 'LI') {
document.querySelector('.tap .add').classList.remove('add')
e.target.classList.add('add')
document.querySelector('.no').classList.remove('no')
content1[+e.target.dataset.id].classList.add('no')
let str = ''
let str1 = ''
let str2 = ''
let str3 = ''
obj.forEach(ele => {
const {url,text,price} = ele
const fn = () => {
return `
<div class="box2">
<img src="${url}" alt="">
<span>${text}</span>
<p>¥${price}</p>
</div>
`
}
str += fn()
if (price < 100) {
str1 += fn()
} else if (price >= 100 && price <= 300) {
str2 += fn()
} else {
str3 += fn()
}
})
console.log(str);
box1[0].innerHTML = str1
box1[1].innerHTML = str2
box1[2].innerHTML = str3
box1[3].innerHTML = str
</script>
</body>
</html>
tap切换2
最新推荐文章于 2024-09-14 18:52:56 发布