下拉
<!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>
<script src="js/jquery-3.6.0.min.js"></script>
<style>
* {
padding: 0;
margin: 0;
}
ul {
list-style: none;
}
.box {
width: 150px;
margin: 0 auto;
}
.box h2 {
position: relative;
width: 150px;
height: 40px;
line-height: 40px;
text-align: center;
background-color: #f00;
}
.box h2 .sj {
position: absolute;
top: 14px;
right: 20px;
width: 0;
height: 0;
border: 10px solid rgba(0, 0, 0, 0);
border-top-color: #222;
}
.box .list {
display: none;
width: 150px;
height: 200px;
box-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
}
</style>
</head>
<body>
<div class="box">
<h2>
标题
<i class="sj"></i>
</h2>
<ul class="list">
<li>列表1</li>
<li>列表2</li>
<li>列表3</li>
</ul>
<h2>
标题
<i class="sj"></i>
</h2>
<ul class="list">
<li>列表1</li>
<li>列表2</li>
<li>列表3</li>
</ul>
</div>
</body>
<script>
// 第一种
// $(document).ready(function () {
// $(".box h2").click(function () {
// var $dis = $(this).next(".list").css("display");
// if ($dis === "none") {
// $(this).next(".list").css("display", "block");
// $(this).children(".sj").css("transform", "rotate(90deg)");
// } else {
// $(this).next(".list").css("display", "none");
// $(this).children(".sj").css("transform", "rotate(0deg)");
// }
// });
// });
// 第二种
// $(document).ready(function () {
// $(".box h2").click(function () {
// $(this)
// .next(".list")
// .show(1000, function () {
// $(this).hide(2000);
// }); //回调函数
// // $(this).next(".list").hide();
// });
// });
// 第三种
// $(document).ready(function () {
// $(".box h2").click(function () {
// var list = $(this).next(".list"); //当前点击的h2后的那一个ul
// list.show(5000, function () {
// // console.log(this);
// $(this).hide(2000); //指向的是有动画的jquery对象
// }); //回调函数
// // $(this).next(".list").hide(5000);
// });
// });
// 第四种
$(document).ready(function () {
$(".box h2").click(function () {
var list = $(this).next(".list"); //当前点击的h2后的那一个ul
list.toggle(200);
});
});
// 显示与隐藏
// jquery对象.show(参数1,参数2)
// 参数1:可选 动画持续时间
// 参数2:可选 回调函数 动画执行完后调用的函数
// jquery对象.hide(参数1,参数2)
// 参数1:可选 动画持续时间
// 参数2:可选 回调函数 动画执行完后调用的函数
</script>
</html>
淡入淡出
<!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>
<script src="js/jquery-3.6.0.min.js"></script>
<style>
* {
padding: 0;
margin: 0;
}
ul {
list-style: none;
}
.box {
width: 150px;
margin: 0 auto;
}
.box h2 {
position: relative;
width: 150px;
height: 40px;
line-height: 40px;
text-align: center;
background-color: #f00;
}
.box h2 .sj {
position: absolute;
top: 14px;
right: 20px;
width: 0;
height: 0;
border: 10px solid rgba(0, 0, 0, 0);
border-top-color: #222;
}
.box .list {
display: none;
width: 150px;
height: 200px;
box-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
}
</style>
</head>
<body>
<div class="box">
<h2>
标题
<i class="sj"></i>
</h2>
<ul class="list">
<li>列表1</li>
<li>列表2</li>
<li>列表3</li>
</ul>
</div>
</body>
<script>
$(function () {
$(".box h2").bind("click", function () {
// 淡入淡出
// 从看不见到看得见 淡入0-1 fadeIn()
// 从看得见到看不见 淡入1-0 fadeOut()
// $(".list").fadeIn(5000).fadeOut(2000);
// $(".list").fadeToggle(2000);
$(".list").fadeTo(2000, 0.5, function () {});
// 往上滑动/往下滑动
// $(".list").slideDown(5000, function () {
// $(this).slideUp(2000);
// });
// $(".list").slideDown(5000);
// $(".list").slideUp(5000);
// $(".list").slideDown(5000).slideUp(5000);
// $(".list").slideToggle("normal");//400
$(".list").slideToggle("slow"); //600
// $(".list").slideToggle("fast"); //200
// $(".list").slideToggle(400);
});
});
// $(function () {
// // // 绑定方式 第一种
// // // $(".box").click(function () {});
// // // dom.onclick=function(){}
// // // 绑定方式 第二种
// // $(".box h2").bind("click", function () {
// // console.log(111);
// // });
// // // 绑定方式 第三种
// // $(".box h2").on("click", function () {
// // console.log(111);
// // });
// // dom.addEventListener("click",function(){})
// });
</script>
</html>