一个CSS+jQuery的放大缩小动画效果

日期: 2013年9月23日

作者:铁锚

// 今天帮朋友写了一些代码,自己觉得写着写着,好几个版本以后,有点满意,于是就贴出来。

// 都是定死了的。因为需求就只有4个元素。如果是要用CSS的class来处理,那就需要用到CSS3动画了。

// 功能 :  在上方的按钮上滑动,可以切换各个page,点击下方的各个page,也可以切换收缩还是展开状态。


初始效果预览


<!DOCTYPE html>
<html>
 <head>
  <title> CSS+jQuery动画效果 </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="铁锚">
  <style>
	body{
		z-index: 0;
		width: 100%;
		min-height: 400px;
	}
	.pages{
		position: absolute;
	}
	.current{
		position: absolute;
		z-index: 12 !important;
		left: 0px !important;
	}
	.page1{
		background-color: #a5cfff;
		z-index: 1;
		width: 300px;
		height:280px;
		top: 100px;
		left: 0px;
	}
	.page2{
		background-color: #b1ca54;
		z-index: 2;
		width: 250px;
		height:270px;
		top: 160px;
		left: 0px;
	}
	.page3{
		background-color: #c2c6c9;
		z-index: 3;
		width: 200px;
		height:260px;
		top: 220px;
		left: 0px;
	}
	.page4{
		background-color: #ef9e9c;
		z-index: 4;
		width: 150px;
		height:250px;
		top: 250px;
		left: 0px;
	}
  </style>
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  <script>
  $(function(){
	// 增长
	function increase($div,e){
		var expstatus = $div.data("expstatus");
		if(!expstatus){
			// 没有展开过
			$div.data("expstatus","yes");
		}
		var style = $div.attr("style");
		$div.addClass("current").attr("styleold",style);
		//
		$div.stop();
		$div.animate({
						opacity:0.9,
						width:"400px",
						height: "400px",
						top: "100px",
						left: "0px"
			},600)
			.animate({
						opacity:1.0
			},30);
		
		e.stopPropagation();
		return false;
	};
	// 还原
	function resize(e){
		// 所有的都移除
		var $page1 = $(".current.page1") ;
		$page1.stop();
		$page1.animate({
						opacity:1.0,
						width:"300px",
						height: "280px",
						top: "100px",
						left: "0px"
			},600,null,function(){
				$page1.removeClass("current").attr("style","");
			});

		var $page2 = $(".current.page2") ;
		$page2.stop();
		$page2.animate({
						opacity:1.0,
						width:"250px",
						height: "270px",
						top: "160px",
						left: "0px"
			},600,null,function(){
				$page2.removeClass("current").attr("style","");
			});

		var $page3 = $(".current.page3") ;
		$page3.stop();
		$page3.animate({
						opacity:1.0,
						width:"200px",
						height: "260px",
						top: "220px",
						left: "0px"
			},600,null,function(){
				$page3.removeClass("current").attr("style","");
			});

		var $page4 = $(".current.page4") ;
		$page4.stop();
		$page4.animate({
						opacity:1.0,
						width:"150px",
						height: "250px",
						top: "250px",
						left: "0px"
			},600,null,function(){
				$page4.removeClass("current").attr("style","");
			});
		//
		
		var expstatus1 = $page1.data("expstatus");
		if(expstatus1){
			$page1.data("expstatus",null);
		}
		var expstatus2 = $page2.data("expstatus");
		if(expstatus2){
			$page2.data("expstatus",null);
		}
		var expstatus3 = $page3.data("expstatus");
		if(expstatus3){
			$page3.data("expstatus",null);
		}
		var expstatus4 = $page4.data("expstatus");
		if(expstatus4){
			$page4.data("expstatus",null);
		}

		if(e){
			e.stopPropagation();
			return false;
		} else {
			return true;
		}
	};
	//
	$("#button1").unbind("mouseover").bind("mouseover",function(e){
		// 
		var $page1 = $(".page1");
		// 添加特定的
		return increase($page1,e);
		
	}).unbind("mouseout").bind("mouseout",function(e){
		return resize(e);
		
	});
	//
	$("#button2").unbind("mouseover").bind("mouseover",function(e){
		// 
		var $page2 = $(".page2");
		// 添加特定的
		return increase($page2,e);
		
	}).unbind("mouseout").bind("mouseout",function(e){
		return resize(e);
	});
	//
	$("#button3").unbind("mouseover").bind("mouseover",function(e){
		// 
		var $page3 = $(".page3");
		// 添加特定的
		return increase($page3,e);
		
	}).unbind("mouseout").bind("mouseout",function(e){
		return resize(e);
	});
	//
	$("#button4").unbind("mouseover").bind("mouseover",function(e){
		// 
		var $page4 = $(".page4");
		// 添加特定的
		return increase($page4,e);
		
	}).unbind("mouseout").bind("mouseout",function(e){
		return resize(e);
	});
	
	//
	$(".pages").unbind("mouseover").bind("mouseover",function(e){
		// 
		var $this = $(this);
		// 添加特定的
		//return increase($this,e);
	}).unbind("mouseout").bind("mouseout",function(e){
		// 所有的都移除
		//return resize(e);
	});
	// 新的
	$(".pages").unbind("click touchstart").bind("click touchstart",function(e){
		// 
		var $this = $(this);
		var expstatus = $this.data("expstatus");
		if(!expstatus){
			// 没有展开过
			//
			return increase($this,e);
		} else {
			return resize(e);
		}
	});
	//
	$("body").click(function(e){
		// 所有的都移除
		return resize(null);
	});
  });
  </script>
 </head>

 <body>
  <div class="pages page1">page1</div>
  <div class="pages page2">page2</div>
  <div class="pages page3">page3</div>
  <div class="pages page4">page4</div>

  <div style="background-color: #a5cfff;">
  <button id="button1">第一页</button>
  <button id="button2">第2页</button>
  <button id="button3">第3页</button>
  <button id="button4">第4页</button>
  </div>
 </body>
</html>


转载于:https://www.cnblogs.com/lanzhi/p/6467093.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的HTML+CSS+jQuery实现平滑动画的代码示例: HTML代码: ``` <!DOCTYPE html> <html> <head> <title>Smooth Animation</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <header> <nav> <ul> <li><a href="#about">About</a></li> <li><a href="#services">Services</a></li> <li><a href="#contact">Contact Us</a></li> </ul> </nav> </header> <main> <section id="about"> <h2>About Us</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec bibendum, nisi vel bibendum vestibulum, turpis ex sodales ipsum, a bibendum urna ex nec tellus.</p> </section> <section id="services"> <h2>Our Services</h2> <div class="service"> <img src="service1.jpg" alt="Service 1"> <p>Service 1 Description</p> </div> <div class="service"> <img src="service2.jpg" alt="Service 2"> <p>Service 2 Description</p> </div> <div class="service"> <img src="service3.jpg" alt="Service 3"> <p>Service 3 Description</p> </div> </section> <section id="contact"> <h2>Contact Us</h2> <form action="#" method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name" required> <label for="email">Email:</label> <input type="email" id="email" name="email" required> <label for="message">Message:</label> <textarea id="message" name="message" required></textarea> <button type="submit">Send Message</button> </form> </section> </main> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="script.js"></script> </body> </html> ``` CSS代码: ``` body { font-family: Arial, sans-serif; margin: 0; padding: 0; } header { background-color: #333; color: #fff; height: 60px; padding: 10px; } nav ul { list-style: none; margin: 0; padding: 0; } nav ul li { display: inline-block; margin-right: 20px; } nav ul li a { color: #fff; text-decoration: none; } main { padding: 50px; } h2 { font-size: 36px; margin-bottom: 20px; } p { font-size: 18px; line-height: 1.5; margin-bottom: 30px; } .service { display: inline-block; margin-right: 20px; vertical-align: top; width: 200px; } .service img { width: 100%; } form label, form input, form textarea { display: block; margin-bottom: 10px; width: 100%; } form input, form textarea { padding: 10px; } form button { background-color: #333; border: none; color: #fff; cursor: pointer; padding: 10px; } form button:hover { background-color: #444; } .hide { display: none; } ``` jQuery代码: ``` $(document).ready(function() { // 平滑滚动到锚点 $('a[href^="#"]').on('click', function(event) { var target = $(this.getAttribute('href')); if (target.length) { event.preventDefault(); $('html, body').stop().animate({ scrollTop: target.offset().top }, 1000); } }); // 滚动到服务区域时显示服务动画 $(window).on('scroll', function() { var servicesTop = $('#services').offset().top; var servicesHeight = $('#services').height(); var scrollTop = $(this).scrollTop(); var windowHeight = $(this).height(); if (scrollTop + windowHeight > servicesTop && scrollTop < servicesTop + servicesHeight) { $('.service').addClass('animated fadeInUp'); } else { $('.service').removeClass('animated fadeInUp'); } }); // 表单提交成功后显示提示信息 $('form').on('submit', function(event) { event.preventDefault(); $('.success').removeClass('hide'); }); }); ``` 注意事项: - 代码中引入了jQuery和animate.css库,需要在页面中先引入才能使用。 - animate.css一个开源的CSS动画库,用于为HTML元素添加动画效果。在本例中使用了其中的fadeInUp动画效果。 - CSS代码仅做简单的样式设置,需要根据实际需求进行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值