仿照(小米官网首页轮播图)特效js代码

最近跟轮播图杠上了,以前也接触过这个轮播图的写法,但是一般都是用插件,久而久之就忘记了!昨晚有学习了一下自己写这个js,虽然是依靠零散的记忆写出来的,可能自己并未真正理解吧!!!以下是我写的代码:
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>小米官网轮播图仿照</title>
	<style type="text/css">
		.container{width: 1226px;height: 460px;margin:0 auto;position: relative;}
		.container img{position: absolute;;top:0;left: 0;display:none;}
		.left,.right{position: absolute;width: 30px;height: 60px;top:50%;margin-top:-30px;color:#fff; line-height: 60px;text-align: center; font-size: 30px;cursor:pointer;}
		.left:hover,.right:hover{background-color: #777171;}
		.left{left:0;}
		.right{right: 0;}
		.container ul{position: absolute;bottom:10px;right:10px;}
		.container ul li{float: left;list-style: none;width: 10px;height:10px;background-color: #fff;border-radius: 100%;border:1px solid #fff;margin-right:10px; line-height: 10px;}
		.container ul li.active{background-color: #776e6e;}
	</style>
</head>
<body>
	<div class="container">
				<img src="images/lb/1.jpg" alt="">
				<img src="images/lb/2.jpg" alt="">
				<img src="images/lb/3.jpg" alt="">
				<img src="images/lb/4.jpg" alt="">
				<img src="images/lb/5.jpg" alt="">
				<div class="left" id="left"><</div>
				<div class="right" id="right">></div>
				<ul>
					<li class="active"></li>
					<li></li>
					<li></li>
					<li></li>
					<li></li>
				</ul>
			</div>		
			<script type="text/javascript">
				var lis=document.getElementsByTagName('li');
				var imgs=document.getElementsByTagName('img');
				imgs[0].style.display='block';
				len=lis.length;
				var index=0;
				for (var i =0; i<len; i++) {
					lis[i].tt=i;
					lis[i].οnclick=function(){
						fadeOut(imgs[index],1);
						index=this.tt;
						changeImg(index);
						fadeIn(imgs[index],1);
					}
				}
				function getId(id){return document.getElementById(id);}
				getId('right').οnclick=function(){
					fadeOut(imgs[index],1);
					index++;
					if (index>len-1) {index=0;}
					changeImg(index);
					fadeIn(imgs[index],1);

				}
				getId('left').οnclick=function(){
					fadeOut(imgs[index],1);
					index--;
					if (index<0) {index=0;alert('已经是第一张了!');}
					changeImg(index);
					fadeIn(imgs[index],1);
					
				}


				function changeImg(dd){

					for(var j=0;j<len;j++){
							lis[j].className='';
							imgs[j].style.display='none';

						}
						lis[dd].className='active';
						imgs[dd].style.display='block';

				}

				// 淡入淡出效果
				function fadeIn(obj,speed){
					obj.style.display='block';
					// obj.style.opacity='0';
					var opa=0;
					var timer=null;
					timer=setInterval(function(){

						opa=opa+speed;
						if (opa>100) {opa=100;clearInterval(timer);}
						obj.style.opacity=opa/100;		
					},13);
				}
				function fadeOut(obj,speed){
					// obj.style.display='none';
					// obj.style.opacity='100';
					var opa='100';
					var timer=null;
					timer=setInterval(function(){

						opa=opa-speed;
						if (opa<0) {opa=0;clearInterval(timer);}
						obj.style.opacity=opa/100;		
					},13);
				}
			</script>
</body>
</html>
照几张图片,放上其实是看,是否能运行吧!
  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是实现一个类似于Vivo官网的导航栏的 HTML + CSS + JS 代码: HTML: ```html <nav class="navbar"> <a href="#" class="navbar-logo">Vivo</a> <ul class="navbar-menu"> <li><a href="#">首页</a></li> <li><a href="#">产品</a></li> <li><a href="#">服务</a></li> <li><a href="#">社区</a></li> <li><a href="#">关于</a></li> </ul> <div class="navbar-search"> <input type="text" placeholder="搜索"> <button>搜索</button> </div> <button class="navbar-toggle"> <span class="navbar-toggle-icon"></span> </button> </nav> ``` CSS: ```css .navbar { display: flex; justify-content: space-between; align-items: center; padding: 20px; background-color: #fff; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); z-index: 999; } .navbar-logo { font-size: 24px; font-weight: bold; color: #333; text-decoration: none; } .navbar-menu { display: flex; justify-content: flex-end; align-items: center; margin: 0; padding: 0; list-style: none; } .navbar-menu li { margin-left: 20px; } .navbar-menu li a { font-size: 16px; color: #333; text-decoration: none; } .navbar-menu li a:hover { color: #f00; } .navbar-search { display: none; } .navbar-search input[type="text"] { border: none; padding: 10px; margin-right: 10px; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } .navbar-search button { border: none; padding: 10px; background-color: #f00; color: #fff; border-radius: 5px; cursor: pointer; } .navbar-toggle { display: none; border: none; background-color: transparent; cursor: pointer; } .navbar-toggle-icon { display: block; width: 20px; height: 2px; background-color: #333; margin: 3px 0; transition: all 0.3s ease-in-out; } .navbar-toggle-icon:first-child { margin-top: 0; } .navbar-toggle-icon:last-child { margin-bottom: 0; } .navbar-fixed { position: fixed; top: 0; left: 0; width: 100%; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } .navbar-fixed .navbar-search { display: block; } .navbar-fixed .navbar-menu { display: flex; flex-direction: column; justify-content: center; align-items: center; position: fixed; top: 70px; left: 0; width: 100%; height: calc(100vh - 70px); background-color: #fff; transition: all 0.3s ease-in-out; z-index: 998; } .navbar-fixed .navbar-menu li { margin-left: 0; margin-bottom: 20px; } .navbar-fixed .navbar-menu li a { font-size: 24px; color: #333; text-decoration: none; } .navbar-fixed .navbar-menu li a:hover { color: #f00; } .navbar-fixed .navbar-toggle { display: block; } .navbar-fixed .navbar-toggle-icon:first-child { transform: rotate(45deg) translate(6px, 6px); } .navbar-fixed .navbar-toggle-icon:last-child { transform: rotate(-45deg) translate(6px, -6px); } .navbar-fixed .navbar-toggle-icon:nth-child(2) { opacity: 0; } ``` JavaScript: ```javascript var navbar = document.querySelector('.navbar'); var navbarOffsetTop = navbar.offsetTop; var navbarToggle = document.querySelector('.navbar-toggle'); function onScroll() { if (window.pageYOffset >= navbarOffsetTop) { navbar.classList.add('navbar-fixed'); } else { navbar.classList.remove('navbar-fixed'); } } window.addEventListener('scroll', onScroll); navbarToggle.addEventListener('click', function() { navbar.classList.toggle('navbar-menu-open'); }); ``` 以上代码实现了一个类似于Vivo官网的导航栏,具有响应式布局和下拉菜单功能。当页面滚动时,导航栏会固定在页面顶部并且搜索框和下拉菜单会出现。在移动设备上,用户可以点击菜单按钮打开下拉菜单。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值