jq 超简单跳楼效果解析

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style type="text/css">
			*{ margin: 0; padding: 0;}
			.list_li{ position: fixed; left: 20px; top: 50%;list-style: none;}
			.list_li li{ width: 20px; height: 20px; border: 1px solid red; }
		</style>
		<script src="js/jquery-2.2.1.min.js"></script>
		<script>
			$(function(){
				$(".list_li").hide();//先让li列表隐藏
				var headTop =$(".head").outerHeight() ;//获取1楼的高度
				$(window).scroll(function(){
					if( $(window).scrollTop() >= headTop  )//判断如果滚动的高度大于1楼的高度 li列表显示
					{
						$(".list_li").show(500);
					}
				})
				
				//跳楼
				$(".list_li >li").click(function(){
					var index = $(this).index();//取到对应li的下标
					
					var oTop = $("div").eq(index).offset().top;//每个对应div 距离顶部的高度
					
					$("body").animate({scrollTop:oTop},1000)//控制body滚动动画
				})
			})
		</script>
	</head>
	<body>
		<div class="head" style="height: 400px; width: 800px; margin: 0 auto; background: red;">一楼</div>
		<div class="head2" style="height: 200px; width: 800px; margin: 0 auto; background: blue;">二楼</div>
		<div style="height: 300px; width: 800px; margin: 0 auto; background: pink;">三楼</div>
		<div style="height: 500px; width: 800px; margin: 0 auto; background: green;">四楼</div>
		<div style="height: 100px; width: 800px; margin: 0 auto; background: blueviolet;">五楼</div>
		<div style="height: 700px; width: 800px; margin: 0 auto; background: orange;">六楼</div>
		<div style="height: 300px; width: 800px; margin: 0 auto; background: palegreen;">七楼</div>
		<div style="height: 900px; width: 800px; margin: 0 auto; background: saddlebrown;">八楼</div>
		<div style="height: 600px; width: 800px; margin: 0 auto; background: darkblue;">九楼</div>
		
		<ul class="list_li">
			<li>1F</li>
			<li>2F</li>
			<li>3F</li>
			<li>4F</li>
			<li>5F</li>
			<li>6F</li>
			<li>7F</li>
			<li>8F</li>
			<li>9F</li>
		</ul>
	</body>
</html>

  

转载于:https://www.cnblogs.com/mingjixiaohui/p/5307811.html

以下是使用jQuery实现简单的拼图效果的示例代码: HTML代码: ```html <div id="puzzle"> <div class="piece" id="piece1"></div> <div class="piece" id="piece2"></div> <div class="piece" id="piece3"></div> <div class="piece" id="piece4"></div> <div class="piece" id="piece5"></div> <div class="piece" id="piece6"></div> <div class="piece" id="piece7"></div> <div class="piece" id="piece8"></div> <div class="piece" id="piece9"></div> </div> ``` CSS代码: ```css #puzzle { width: 300px; height: 300px; border: 1px solid #000; position: relative; } .piece { width: 100px; height: 100px; border: 1px solid #000; background-image: url('path/to/image.jpg'); background-size: 300px 300px; position: absolute; } #piece1 { top: 0; left: 0; } #piece2 { top: 0; left: 100px; } #piece3 { top: 0; left: 200px; } #piece4 { top: 100px; left: 0; } #piece5 { top: 100px; left: 100px; } #piece6 { top: 100px; left: 200px; } #piece7 { top: 200px; left: 0; } #piece8 { top: 200px; left: 100px; } #piece9 { top: 200px; left: 200px; } ``` jQuery代码: ```javascript $(document).ready(function() { var emptyPiece = $('#piece9'); // 空白拼图块 var pieces = $('.piece'); // 所有的拼图块 // 随机打乱拼图块的位置 pieces.sort(function() { return Math.random() - 0.5; }); // 设置每个拼图块的位置 pieces.each(function(index) { var piece = $(this); var top = Math.floor(index / 3) * 100; var left = (index % 3) * 100; piece.css({ top: top + 'px', left: left + 'px' }); // 点击拼图块时进行交换 piece.click(function() { var currentTop = parseInt(piece.css('top')); var currentLeft = parseInt(piece.css('left')); // 判断是否与空白拼图块相邻 if ((currentTop === emptyPiece.position().top && Math.abs(currentLeft - emptyPiece.position().left) === 100) || (currentLeft === emptyPiece.position().left && Math.abs(currentTop - emptyPiece.position().top) === 100)) { var tempTop = piece.css('top'); var tempLeft = piece.css('left'); piece.css({ top: emptyPiece.css('top'), left: emptyPiece.css('left') }); emptyPiece.css({ top: tempTop, left: tempLeft }); } }); }); }); ``` 在上面的示例代码中,我们首先定义了一个包含9个拼图块的容器,并使用CSS设置了每个拼图块的位置。然后在jQuery代码中,我们将所有的拼图块随机打乱位置,并为每个拼图块绑定了一个点击事件,在点击拼图块时进行交换。交换拼图块时,我们使用了jQuery的css()方法来修改每个拼图块的位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值