js案例
鼠标光标异动不同数字,窗口内容翻页
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>testing</title>
<style type="text/css">
.wrapper {
width: 50px;
height: 50px;
border: 1px solid pink;
overflow: hidden; // 把多余溢出窗口的内容隐藏掉
}
</style>
<script src="jquery-3.5.1.min.js"></script>
<script>
$(function () {
$("div h5").mouseover(function () {
var index = $(this).index();
console.log(index);
$(".wrapper p").eq(index).show().siblings().hide();
});
})
</script>
</head>
<div class="wrapper">
<p>test1</p>
<p>test2</p>
<p>test3</p>
</div>
<div>
<h5>1</h5> //鼠标在 1 2 3上切换,js脚本会获取每个标签的index,来决定显示哪条。淘宝橱窗应用此技术
<h5>2</h5>
<h5>3</h5>
</div>
</html>
给超链接添加背景以及鼠标悬浮背景切换,css组合
#left li {
background: url(images/lili.jpg) repeat-x;
}
#left li a {
display: block;
width: 48px;
height: 27px;
border-bottom: 1px solid pink;
line-height: 27px;
text-align: center;
color: white;
}
#left li a:hover {
background-image: url(images/abg.gif);
}
空格和 “>”
E > F 表示选择E元素的所有子F元素,与E F的区别在于,E F选择所有后代元素,>只选择一代
标题选中切换显示不同内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<script src="jquery-3.5.1.min.js"></script>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
li{
list-style-type: none;
}
.tab{
width: 978px;
margin: 100px auto;
}
.tab_list{
height: 39px;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
.tab_list li{ /*标题的li标签依次左右排列起来*/
float: left;
padding: 0 20px;
line-height: 39px;
text-align: center;
cursor: pointer;
}
.tab_con{
border: 1px solid turquoise;
}
.item{
display: none; //默认不显示
}
.tab_list .current{
background-color: #c81623;
color: white; /*这是字体颜色*/
}
</style>
</head>
<body>
<div class="tab">
<div class="tab_list">
<ul>
<li class="current">商品介绍</li>
<li>规格与包装</li>
<li>售后保障</li>
<li>商品评价(50000)</li>
<li>手机社区</li>
</ul>
</div>
<div class="tab_con">
<div class="item" style="display: block">
商品介绍模块内容
</div>
<div class="item">
规格与包装模块内容
</div>
<div class="item">
售后保障模块内容
</div>
<div class="item">
商品评价(50000)模块内容
</div>
<div class="item">
手机社区模块内容
</div>
</div>
</div>
<script>
$(function () {
$(".tab_list li").click(
function () {
$(this).addClass("current").siblings().removeClass("current");
var index = $(this).index();
console.log(index);
$(".tab_con .item").eq(index).show().siblings().hide();
}
);
});
</script>
</body>
</html>
高亮显示
(图片自行找个长条图就行)
<!doctype html>
<html lang="en">
<head>
<title>
index
</title>
<meta charset="UTF-8">
<script src="jquery-3.5.1.min.js"></script>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
li{
list-style-type: none;
}
body{
background-color: black;
}
.wrapper{
margin: 100px auto 0; /*左右auto 会居中对齐*/
width: 630px;
height: 330px;
border: white 1px solid;
padding: 10px 0 0 15px;
overflow: hidden;
}
.wrapper li img{
float: left;
height: 55px;
width: 100%;
display: block;
/* margin: 0 10px 10px 0; 因为宽度放这边已经是100%,放在li标签才能有间距效果*/
}
.wrapper li{
margin: 0 0 10px 0;
float: left;
}
</style>
</head>
<body>
<div class="wrapper">
<ul>
<li><a href="#"><img src="./images/lili.jpg" alt="test"></a></li>
<li><a href="#"><img src="./images/lili.jpg" alt="test"></a></li>
<li><a href="#"><img src="./images/lili.jpg" alt="test"></a></li>
<li><a href="#"><img src="./images/lili.jpg" alt="test"></a></li>
<li><a href="#"><img src="./images/lili.jpg" alt="test"></a></li>
</ul>
</div>
<script>
$(function () {
$(".wrapper li").hover(
//鼠标进入时的渲染效果
function () {
$(this).siblings().stop().fadeTo(400,0.5);
},
//鼠标移开时渲染效果
function () {
$(this).siblings().stop().fadeTo(400,1);
}
)
})
</script>
</body>
</html>
效果:鼠标指向的条,高亮显示
单选框复选框
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.content{
width: 500px;
border: 1px solid red;
margin: 0 auto;
}
.a{
border-bottom: 1px solid green;
}
.b .j-checkbox{
display: block; /*这个display block只对input这个标签有效*/
margin: 20px 3px;
}
.c{
border-top: 1px solid yellow;
}
.a,.b,.c{
margin: 50px 0;
}
</style>
<script src="jquery-3.5.1.min.js"></script>
</head>
<body>
<div class="content">
<div class="a">
<label>
<input type="checkbox" class="checkall">
</label>
</div>
<div class="b">
<label>
<input type="checkbox" class="j-checkbox">
<input type="checkbox" class="j-checkbox">
<input type="checkbox" class="j-checkbox">
</label>
</div>
<div class="c">
<label>
<input type="checkbox" class="checkall">
</label>
</div>
</div>
<script>
$('.checkall').change(function() {
$(".j-checkbox,.checkall").prop("checked", $(this).prop("checked")); //checkbox 本身就具有checked这个属性,选中为true 取消为false
console.log($(this).prop("checked"))
});
$('.j-checkbox').change(function() {
if ($('.j-checkbox:checked').length === $('.j-checkbox').length) {
$(".checkall").prop("checked", true);
} else {
$(".checkall").prop("checked", false);
}
console.log($('.j-checkbox:checked').length);
console.log($('.j-checkbox').length);
});
</script>
</body>
</html>
返回顶部功能
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
height: 1500px;
}
.container { /*主容器*/
width: 900px;
height: 500px;
background-color: skyblue;
margin: 200px auto;
}
.back { /*底部容器*/
position: fixed;/*元素的位置相对于浏览器窗口是固定位置 即使窗口是滚动的它也不会移动*/
width: 1000px;
height: 50px;
background-color: pink;
left: 500px;
bottom: 100px;
}
</style>
<script src="jquery-3.5.1.min.js"></script>
</head>
<body>
<div class="back">返回顶部</div>
<div class="container">
</div>
<script>
$(function() {
$(document).scrollTop(100);
// 被卷去的头部 scrollTop() / 被卷去的左侧 scrollLeft()
// 页面滚动事件
var boxTop = $(".container").offset().top;
console.log(boxTop);
$(window).scroll(function() {
// console.log(11);
console.log($(document).scrollTop());
if ($(document).scrollTop() >= boxTop) { //scrollTop 是返回被选元素的垂直滚动条位置
$(".back").fadeIn(); //淡入 显示出来
} else {
$(".back").fadeOut();//淡出 隐藏起来
}
});
// 返回顶部
$(".back").click(function() {
// $(document).scrollTop(0);
$("body, html").animate({
scrollTop: 0
});
})
})
</script>
</body>
</html>
滑块点击及滚动效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.slider-bar {
position: fixed;
right: 0;
top: 50%;
/*margin-left: 600px;*/ /*right去掉后才能生效*/
display: none;
}
.a,
.b,
.c {
width: 150px;
height: 50px;
line-height: 50px;
text-align: center;
border: 1px solid black;
cursor: pointer;
}
.w {
width: 1200px;
margin: 10px auto; /*这是外边距*/
}
.header {
height: 150px;
background-color: purple;
}
.banner {
height: 250px;
background-color: skyblue;
}
.green {
height: 500px;
background-color: green;
}
.blue {
height: 500px;
background-color: blue;
}
.black {
height: 500px;
background-color: black;
}
.selected {
background-color: red;
color: #FFFFFF;
}
.floot {
height: 1000px;
}
</style>
<script src="jquery-3.5.1.min.js"></script>
</head>
<body>
<div class="slider-bar">
<div class="a selected">绿色</div>
<div class="b">蓝色</div>
<div class="c">黑色</div>
</div>
<div class="header w"></div>
<div class="banner w"></div>
<div class="main">
<div class="green w"></div>
<div class="blue w"></div>
<div class="black w"></div>
</div>
<div class="floot">
</div>
<script>
function toggleTllo() {
if ($(document).scrollTop() >= $(".banner").offset().top) {
$(".slider-bar").fadeIn();
} else {
$(".slider-bar").fadeOut();
}
}
var flag = true;
$(window).scroll(function() {
// console.log($(document).scrollTop());
// console.log($(".banner").offset().top);
toggleTllo();
if (flag) {
$(".main div").each(function(i, ele) {
// console.log($(document).scrollTop());
// console.log($(ele).offset().top);
if ($(document).scrollTop() >= $(ele).offset().top) {//each() 方法为每个匹配元素规定要运行的函数。 三个div依次触发
$(".slider-bar div").eq(i).addClass("selected").siblings().removeClass("selected");
}
});
}
});
$(".slider-bar div").click(function() {
flag = false; /*这个flag是为了点击animate偏移的时候使toggleTllo失效*/
var wz = $(".main div").eq($(this).index()).offset().top;
$("body,html").stop().animate({
scrollTop: wz
},function(){
flag = true;
});
$(this).addClass("selected").siblings().removeClass("selected");
});
</script>
</body>
</html>
悬停显示提示框
鼠标放在提示信息上 显示出提示框,移开后隐藏
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en">
<head>
<title>鼠标悬停显示提示信息窗口</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<style type="text/css">
.contact{
width: 80px;
height:20px;
padding: 2px;
margin:20px 0 10px 50px;background-color:#CCCCCC;
text-align:center;
}
.us{display:none;width:300px;height:40px;
padding:10px;position:relative;top:0;left:50px;
background-color:#0099FF;
}
</style>
<script src="jquery-3.5.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".contact").mouseover(function(){
$(".us").show("slow");
$(".test").hide("slow");
$(".contact").mouseout(function(){
$(".us").hide("slow");
$(".test").show("slow");
});
});
})
</script>
</head>
<body>
<div class="contact">联系我们</div>
<div class="us">提示内容!提示内容!提示内容!</div>
<div class="test">上面会显示悬浮内容</div>
</body>
</html>
电影排行榜
<!DOCTYPE html>
<html lang="en">
<head>
<title>js测试案例</title>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8"/>
<style type="text/css">
* {padding: 0;
margin: 0;}
.box{
width: 300px;
height: 450px;
margin: 50px auto;
border: 1px solid #000;
}
.box>h1{
font-size: 20px;
line-height: 35px;
color: deeppink;
padding-left: 10px;
border-bottom: 1px dashed #ccc;
}
ul>li{
list-style: none;padding: 5px 10px;
border: 1px dashed #ccc;
}
ul>li:nth-child(-n+3) span{
background: deeppink;
}
ul>li>span{
display: inline-block;
width: 20px;
height: 20px;
background: #ccc;
text-align:center;
line-height: 20px;
margin-right: 10px;
}
.content{
overflow: hidden;
display: none;
}
.content>img{
width: 80px;
height: 100px;
float: left;
}
.content>p{
width: 180px;
height: 100px;
float: right;
font-size: 12px;
line-height: 20px;
}
.current .content{ /*current下的content才能有block效果*/
display: block;
}
</style>
<script src="jquery-3.5.1.min.js"></script>
<script>
$(function() {
$("li").hover(function(){
$(this).addClass("current")
},function(){
$(this).removeClass("current")
})
})
</script>
</head>
<body>
<div class="box">
<h1>电影排行榜</h1>
<ul>
<li><span>1</span>战狼1
<div class="content">
<img src="https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=1075481921,1149912994&fm=58&w=121&h=140&img.JPEG">
<p>
简介:故事发生在非洲附近的大海上,主人公冷锋(吴京 饰)遭遇人生滑铁卢,被“开除军籍”,本想漂泊一...
</p>
</div>
</li>
<li><span>2</span>战狼2
<div class="content">
<img src="https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=1075481921,1149912994&fm=58&w=121&h=140&img.JPEG">
<p>
简介:故事发生在非洲附近的大海上,主人公冷锋(吴京 饰)遭遇人生滑铁卢,被“开除军籍”,本想漂泊一...
</p>
</div>
</li>
<li><span>3</span>战狼3</li>
<li><span>4</span>战狼4</li>
<li><span>5</span>战狼5</li>
<li><span>6</span>战狼6</li>
<li><span>7</span>战狼7</li>
</ul>
</div>
</body>
</html>
效果
两侧对联式弹窗
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
* {
padding: 0;
margin: 0;
}
body{
height: 2500px;
}
.left{
float: left;
position: fixed;
left: 0;
top: 200px;
}
.right{
float: right;
position: fixed;
right: 0;
top: 200px;
}
img{
display: none;
}
</style>
<script src="jquery-3.5.1.min.js"></script>
<script>
$(function() {
$(window).scroll(function(){
// 网页滚动偏移位
var offset = $("html,body").scrollTop();
if(offset>=800){
$("img").show(1000)
}else{
$("img").hide(1000)
}
console.log(offset)
})
})
</script>
</head>
<body>
<img src="https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1906469856,4113625838&fm=26&gp=0.jpgg" class="left">
<img src="https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1906469856,4113625838&fm=26&gp=0.jpg" class="right">
</body>
</html>
效果(下滑滚动条,滚动800px弹出)
菜单展示方式一
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8"/>
<style type="text/css">
* {
padding: 0;
margin: 0;
}
.nav{
list-style: none;
width: 300px;
height: 50px;
background: red;
margin: 100px auto;
}
.nav>li{
width: 100px;
height: 50px;
line-height: 50px;
text-align: center;
float: left;
}
.current{
list-style: none;
background: blue;
display: none;
}
</style>
<script src="jquery-3.5.1.min.js"></script>
<script>
$(function() {
//监听一级菜单的移入事件
$(".nav>li").mouseenter(function(){
var $current = $(this).children(".current");
//让当前正在运行的动画停止,
$current.stop();
//展示二级菜单
$current.slideDown()
});
$(".nav>li").mouseleave(function(){
var $current = $(this).children(".current");
$current.stop();
$current.slideUp()
})
})
</script>
</head>
<body>
<ul class="nav">
<li>一级菜单
<ul class="current">
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
<li>一级菜单
<ul class="current">
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
<li>一级菜单
<ul class="current">
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
</ul>
</body>
</html>
菜单展示二
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<style type="text/css">
* {
padding: 0;
margin: 0;
}
.nav{
list-style: none;
width: 300px;
margin: 100px auto;
/*border: 1px solid #000;*/
}
.nav>li{
border: 1px solid #000;
line-height: 35px;
border-bottom: none;
text-indent: 2em;
}
.nav>li:last-child{
border-bottom: 1px solid #000;
border-bottom-right-radius: 10px;
border-bottom-left-radius: 10px;
}
.nav>li:first-child{
border-top-right-radius: 10px;
border-top-left-radius: 10px;
}
.nav>li>span{
float: right;
padding-right: 20px;
}
.sub{
display: none;
}
.sub>li{
list-style: none;
background: mediumpurple;
border-bottom: 1px solid white;
}
.sub>li:hover{
background: red;
}
.nav>.current>span{
transform: rotate(90deg); /*旋转90度*/
}
</style>
<script src="jquery-3.5.1.min.js"></script>
<script>
$(function() {
//监听一级菜单 点击事件
$(".nav>li").click(function(){
// 拿到二级菜单
var $sub = $(this).children(".sub");
$sub.slideDown();
//拿到非当前的二级菜单 ,让非当前的二级菜单收起
var otherSub = $(this).siblings().children(".sub");
otherSub.slideUp();
//让箭头90度旋转
$(this).addClass("current");
$(this).siblings().removeClass("current")
})
})
</script>
</head>
<body>
<ul class="nav">
<li>一级菜单<span>></span>
<ul class="sub">
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
<li>一级菜单<span>></span>
<ul class="sub">
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
<li>一级菜单<span>></span>
<ul class="sub">
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
</ul>
</body>
</html>
弹窗广告
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<style type="text/css">
* { padding: 0; margin: 0; }
.ad{ position: fixed; right: 0; bottom:0; display: none; }
.ad>span{ display: inline-block; width: 30px; height: 30px; /*background: red;*/ position: absolute; top: 0; right: 0; }
.ad>img{
width: 200px;
height: 200px;
}
</style>
<script src="jquery-3.5.1.min.js"></script>
<script>
$(function() {
$("span").click(function(){
$(".ad").remove();
});
// 动画之前先调用 stop(),避免出现动画队列
// slideDown()、fadeOut()、fadeIn()参数1动画耗时,参数2动画完毕执行的动作
// $(".ad").stop().slideDown(1000,function(){
// $(".ad").fadeOut(1000, function(){
// $(".ad").fadeIn(1000)
// })
// });
$(".ad").stop().slideDown(1000).fadeOut(1000).fadeIn(2000) /*slideDown() 适用于通过 jQuery 方法隐藏的元素,或在 CSS 中声明 display:none 隐藏的元素(不适用于通过 visibility:hidden 隐藏的元素)。*/
});
</script>
</head>
<body>
<div class="ad">
<img src="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1498827195,3078250300&fm=26&gp=0.jpg" alt="">
<span></span>
</div>
</body>
</html>
animate的使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
* {
padding: 0;
margin: 0;
}
div{
width: 100px;
height: 100px;
margin-top: 10px;
background: red;
}
.two{
background: blue;
}
</style>
<script src="jquery-3.5.1.min.js"></script>
<script>
$(function() {
$("button").eq(0).click(function(){
$(".one").animate({
width:50
},2000);
$(".two").animate({
marginLeft:500
},2000);
});
$("button").eq(1).click(function(){
$(".one").animate({
width:"+=100"
},2000)
});
$("button").eq(2).click(function(){
$(".one").animate({
width:"hide" /*只要是animate里面的就是渐变的动作*/
},2000)
});
$("button").eq(3).click(function(){
$(".one").animate({
width:"toggle" /*如果内容显示,width: "toggle" 将会结果隐藏DIV及内容;如果内容隐藏,width: "toggle" 将会结果显示DIV及内容;*/
},2000)
})
});
</script>
</head>
<body>
<button>操作属性</button>
<button>累加属性</button>
<button>关键字hide</button>
<button>关键字toggle</button>
<div class="one"></div>
<div class="two"></div>
</body>
</html>