博主信息
博文 40
粉丝 2
评论 1
访问量 46466
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
16.jQuery滚动条触发与返回顶部按钮-2019年01月22号
万物皆对象
原创
1046人浏览过

实例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>1.当滚动条下拉时触发事件</title>
	<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
	<style>
		*{margin: 0;padding: 0;}
		.content{
			width: 100%; height: 60px;
			background: rgba(160, 3, 162, 0.1);
			box-shadow: 1px 3px 7px #ccc;
			line-height: 60px;
			position: fixed; /* 固定定位 */
		}
		.content-2{
			background: rgba(160, 3, 162, 0.1);
			/* background: red; */
		}
		form{
			width: 500px; height: 35px;
			position: relative; /* 相对定位 */
			margin: 0 auto;
		}
		input{
			width: 480px; height: 35px;
			border-radius: 20px;
			border: none;  
			outline: none; /* 去掉自带焦点 */
			padding-left: 20px; /* 将input框里面的光标往右移动 */
		}
		button{
			width: 70px; height: 35px;
			order: none;
			border: none;
			border-top-right-radius: 20px;
			border-bottom-right-radius: 20px;
			color: #fff;
			position: absolute; /* 绝对定位 */
			right: 0;
			top: 13.4px;
			outline: none;
			font-weight: bold;
			background: rgba(160, 3, 162, 0.4);
		}
		[placeholder]{
			color: rgba(160, 3, 162, 0.6);
		}
		.slide{
			width: 70%; height: 400px;
			background: url(3.jpg);
			margin: 0 auto;
		}
		.box{
			width: 70%; height: 1200px;
			background: rgba(92, 88, 92, 0.1);
			margin: 0 auto;
		}
		a{text-decoration:none;color:red;font-weight:bold;}
		.r-menu{
			position:fixed;
			width:50px;
			height:50px;
			background:pink;
			bottom:50px;
			right:10px;
			border-radius:50%;
			line-height:50px;
			text-align:center;
			display:none;
		}
	</style>
</head>
<body>
	<!-- 顶部导航navigation -->
	<div class="content">
		<form>
			<input type="text" placeholder="请输入搜索内容...">
			<button>搜索</button>
		</form>
	</div>
	<!-- 轮播图 -->
	<div class="slide"></div>
	<!-- 主体内容 -->
	<div class="box"></div>
	<!-- 返回顶部按钮 -->
	<div class="r-menu">
		<a href="javascript:;" id="backTop">顶部</a>
	</div>
</body>
</html>
<script>
	$(document).ready(function(){
		$(window).scroll(function(){
			// 判断滚动条向下滚动是否大于60
			if($(window).scrollTop() > 60){
				$('.content').hide(); // 大于60就隐藏导航栏
			}else{
				$('.content').show(); // 反之就是小与60,为显示
			}

			// 判断滚动条向下滚动是否大于400
			if($(window).scrollTop() > 400){
				// 大于400就添加样式和设置为块级元素
				$('.content').addClass('content-2').css('display','block');
			}else{
				// 反之删除元素
				$('.content').removeClass('content-2');
			}
		});
		
		// 进度条大于100的时候显示返回顶部的按钮
		$(window).on('scroll',function(){
			if($(window).scrollTop() > 100){
				$('.r-menu').fadeIn(200);
			}else{
				$('.r-menu').fadeOut(200);
			}
		});
		// 点击顶部按钮时返回到浏览器顶部
		$('#backTop').click(function(){
			$('html,body').animate({scrollTop:'0'},100);
		});
	});
</script>

运行实例 »

点击 "运行实例" 按钮查看在线实例


素材: 3.jpg

3.jpg

批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学