Files
ClassContent/历届学生/fontendsix/项目练习/上课项目/tabs切换/插件版.html
2024-09-27 02:06:13 +08:00

161 lines
4.4 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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>tabs切换</title>
<style>
body,
ul,
li {
margin: 0;
padding: 0;
list-style: none;
}
.tabs_title {
display: flex;
}
.tabs_title li {
margin-right: 20px;
}
.title_active {
color: red;
}
.tabs_content ul {
display: none;
}
.content_active {
display: block !important;
}
</style>
</head>
<body>
<ul class="tabs_title">
</ul>
<div class="tabs_content">
</div>
</body>
<script>
// 需要两个数组 存放什么数据?
// 第一个数组:存放标题的li标签
// 第二个数组:存放标题与之对应的内容的标签
// 事件 (悬浮,点击事件)
// 为 第一个数组中所有li标签 批量 绑定事件
// 当事件触发的时候,获取用户 点击 的那个li标签的数组下标
// 用下标来控制 标题和内容的css类名
var tabs = [
{
title: '精选1',
content: [
{ text: '精选1的标题', content: '精选1的简介', time: '精选1的发布时间' },
{ text: '精选2的标题', content: '精选2的简介', time: '精选2的发布时间' },
]
},
{
title: '美食',
content: [
{ text: '美食1的标题', content: '美食1的简介', time: '美食1的发布时间' },
{ text: '美食2的标题', content: '美食2的简介', time: '美食2的发布时间' },
{ text: '美食3的标题', content: '美食3的简介', time: '美食3的发布时间' },
]
},
{
title: '百货',
content: [
{ text: '百货1的标题', content: '百货1的简介', time: '百货1的发布时间' },
]
},
{
title: '个护',
content: [
{ text: '个护1的标题', content: '个护1的简介', time: '个护1的发布时间' },
{ text: '个护2的标题', content: '个护2的简介', time: '个护2的发布时间' },
{ text: '个护3的标题', content: '个护3的简介', time: '个护3的发布时间' },
{ text: '个护4的标题', content: '个护4的简介', time: '个护4的发布时间' },
{ text: '个护5的标题', content: '个护5的简介', time: '个护5的发布时间' },
{ text: '个护6的标题', content: '个护6的简介', time: '个护6的发布时间' },
]
}
];
// 根据数据渲染生成标题的li标签
function renderTitle() {
tabs.forEach((v, i) => {
$(".tabs_title").innerHTML += `<li class="${i == 0 ? 'title_active' : ''}">${v.title}</li>`
});
renderContent();
var tabsTitle = document.querySelectorAll(".tabs_title li")
var tabsContent = document.querySelectorAll(".tabs_content ul")
tabsTitle.forEach((v, i) => {
v.onclick = () => {
tabsTitle.forEach( (val, index) => {
val.className = ""
tabsContent[index].className = ""
})
v.className = "title_active"
tabsContent[i].className = "content_active"
}
})
}
function renderContent() {
tabs.forEach((v, i) => {
$(".tabs_content").innerHTML += `
<ul class="${i == 0 ? 'content_active' : ''}">
${v.content.map((item, index) => {
return `
<li>
<h2>${item.text}</h2>
<span>${item.time}</span>
<p>${item.content}</p>
</li>
`
}).toString().replaceAll(",", "")}
</ul>
`
});
}
function $(className) {
return document.querySelector(className)
}
renderTitle();
//es6 function 简写 箭头函数
// var a = function (test) {
// return test * 2;
// }
// var a = test => test * 2
// a(3,4)
</script>
</html>