Day17-小功能系列:盒子的旋转

这篇博客介绍了一个小功能——使用JavaScript实现盒子旋转效果。当鼠标悬停时,盒子顺时针旋转,移开时逆时针旋转回到初始位置。通过HTML创建基本结构,CSS设置圆形样式,JavaScript实现旋转动画,利用全局变量跟踪旋转角度并设定条件停止旋转。在处理鼠标移入移出事件时,注意清除定时器以避免冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Day17-小功能系列:盒子的旋转


功能描述:当鼠标放在盒子上,盒子进行顺时针转动,转动完一圈后停止;当鼠标移开,盒子进行逆时针转动,回到初始位置停止。


1.HTML界面

前端界面和简单,随便创建一个div

<div class="test">这是旋转的盒子</div>

2.Css样式

主要将创建的div变成一个圆形,当然可有可无。

.test{
	width:100px;
	height:100px;
	text-align:center;
	font-size:12px;
	line-height:100px;
	border:1px solid #000;
	border-radius:50px;
}

3.JavaScript代码

主要的功能实现。
程序思路:首先获取这个div并添加hover方法。【注:hover方法包括两个函数,一个移入,一个移出】

$(".test").hover(function(){},function(){});

在移入方法中再次获取div,并添加css样式:transform:rotate()。

$(".test").hover(function(){
	$(".test").css({transform:"rotate()"});
},function(){});

考虑到rotate的值要一直变化,所以声明一个全局变量“i”来记录角度。并且声明一个定时器“Time”来使角度发生变化。

var i = 0; //初始化i
var Time;  //记录定时器
$(".test").hover(function(){
	Time = setInterval(function(){
		//使用i来记录角度
		$(".test").css({transform:"rotate("+i+"deg)"});
		i++;
	},1)
},function(){});

已经完成旋转的功能了,接着要考虑到旋转一周期停止,则需要添加if(){}判断。

var i = 0; //初始化i
var Time;  //记录定时器
$(".test").hover(function(){
	Time = setInterval(function(){
		//使用i来记录角度
		$(".test").css({transform:"rotate("+i+"deg)"});
		if(i >= 360){
			clearInterval(Time);
		}
		i++;
	},1)
},function(){});

为什么将i++放在判断后面?
因为如果放在前面,那么定时器会在360的时候多执行一轮,故放在判断后面。

接下来,如法炮制,用同样的思路写移出方法,把移入的i++改为i–;并且更改判断语句。

var i = 0; //初始化i
var Time;  //记录定时器
$(".test").hover(function(){
	Time = setInterval(function(){
		//使用i来记录角度
		$(".test").css({transform:"rotate("+i+"deg)"});
		if(i >= 360){
			clearInterval(Time);
		}
		i++;
	},1)
},function(){
	Time = setInterval(function(){
			//使用i来记录角度
			$(".test").css({transform:"rotate("+i+"deg)"});
			if(i >= 0){
				clearInterval(Time);
			}
			i--;
		},1)
});

之后还会遇到一个问题,就是鼠标一边移入一边移出,这时函数会同时执行多个定时器,所以在执行移入移出两个方法时,要清除各自的定时器。
并且方法要在加载方法中执行。

var i = 0; //初始化i
var Time;  //记录定时器

$(function(){
	$(".test").hover(function(){
		Time = setInterval(function(){
			//清除各自定时器
			clearInterval(Time);
			//使用i来记录角度
			$(".test").css({transform:"rotate("+i+"deg)"});
			if(i >= 360){
				clearInterval(Time);
			}
			i++;
		},1);
	},function(){
		Time = setInterval(function(){
				//清除各自定时器
				clearInterval(Time);
				//使用i来记录角度
				$(".test").css({transform:"rotate("+i+"deg)"});
				if(i >= 0){
					clearInterval(Time);
				}
				i--;
			},1);
	});
});

完成

<style>/* 底部状态栏 */ .fliale { z-index: 1; border-top: 0.02rem solid #e6e6e6; width: 100%; height: 1.25rem; position: fixed; bottom: 0rem; display: flex; flex-direction: row; justify-content: space-around; align-items: center; }button#NotJob { width: 100%; height: 100%; font-size: 0.40rem; border: none; background-color: #ffffff; } button#GoodJob { width: 100%; height: 100%; font-size: 0.40rem; border: none; background-color: #ffffff; } span.countJia { margin: 0.2rem; border-radius: 3.05rem; border: none; background-color: red; padding: 0.1rem 0.2rem; } span.countJan { color: white; margin: 0.2rem; border-radius: 3.05rem; border: none; background-color: #00f300; padding: 0.1rem 0.2rem; }.BtnTR { z-index: 1; position: fixed; bottom: 1.85rem; height: 1.5rem; width: 4.1rem; background-color: #008cf3; border: none; color: white; font-size: 0.55rem; border-radius: 0.08rem; font-family: "黑体"; box-shadow: 0px 5px 9px 0px #484b5157; }div#inputs-Scoll { top:-0.3rem; position: relative; overflow-x: hidden; overflow-y: scroll; display: flex; height: 16.7rem; width: 100%; flex-direction: column; align-items: center; } .InputDatas { position: fixed; top: 0; padding: 0.25rem 0rem; display: flex; border-bottom: 1px solid #aaaaaa; height: 1.12rem; background-color: white; width: 100%; flex-direction: row; align-items: center; justify-content: center; } .BDSS{ box-shadow: 0.01rem 0.04rem 0.1rem #d7d7d7; font-size: 0.35rem; border-radius: 0.08rem; padding: 0.25rem; background-color: white; margin: 0.05rem 0.2rem; } .BDS_stateWhy { display: flex; align-items: center; } input.BDS_decserText { padding: 0rem 0rem; margin: 0rem 0.15rem; border: none; font-size: 0.35rem; } button.BDS_decserBtn{ color: #0093ff; background-color: #f0fff200; font-size: 0.35rem; border: none; width: 0.90rem; } button.BDS_decserBtn:hover{ color: #bbbbbb; } .BDALL{ width: 100%; display: flex; flex-direction: column; } input.AllDatas { margin-right: 0.16rem; text-align: center; font-size: 0.35rem; border-radius: 0.08rem; border: 0.02rem solid #989898; background-color: #f7f7f7; height: 0.7rem; width: 5.22rem; color: #5f5f5f; } .BtnT:hover { background-color: #0066ff; }</style><body> <div class="conter"> </div> </body><script>let dataJsons = []; let dataJsonsErr =[ { "cardid": "0422AA12361E90", "building": "B栋", "floor": "1F", "area": "电气室", "project": "风扇1", "status":"OK", "msg":"", "checkTime":"2025.6.27-15:41:22" }, { "cardid": "0423AA12361E90", "building": "B栋", "floor": "1F", "area": "电气室", "project": "风扇2", "status":"OK", "msg":"", "checkTime":"2025.6.25-17:21:32" }, { "cardid": "0424AA12361E90", "building": "B栋", "floor": "2F", "area": "电气室", "project": "风扇3", "status":"NG", "msg":"异常报错2", "checkTime":"2025.6.24-10:11:08" },{ "cardid": "0425AA12361E90", "building": "B栋", "floor": "1F", "area": "电气室", "project": "风扇13", "status":"OK", "msg":"", "checkTime":"2025.6.25-17:21:32" }, { "cardid": "0426AA12361E90", "building": "B栋", "floor": "2F", "area": "电气室", "project": "风扇12", "status":"NG", "msg":"异常报错2", "checkTime":"2025.6.24-10:11:08" }, { "cardid": "0424AA12361E90", "building": "B栋", "floor": "2F", "area": "电气室", "project": "风扇3", "status":"NG", "msg":"异常报错2", "checkTime":"2025.6.24-10:11:08" }]; $(".conter").append( '<div class="fliale">'+ '<button type="button" id="NotJob">'+'未完成'+'<span class="countJia">'+ dataJsons.length +'</span>'+'</button>'+ '<button type="button" id="GoodJob">'+'已完成'+'<span class="countJan">'+ dataJsonsErr.length +'</span>'+'</button>'+ '</div>'); $(".conter").append( '<div class="InputDatas">'+ '<input class="AllDatas" id="searchInput" type="text" placeholder="输入搜索内容" value="" />'+ '<span id="resultCount">'+'</span>'+ '</div>'+ '<div class="u4Scoll" id="inputs-Scoll">'+ '<div class="BDALL">'+ '</div>'+ '</div> ');$("body").append('<button class="BtnTR">'+'提交任务'+'</button>'); $(".BtnTR").click(function(){ //其他代码不用管 }) $.each(dataJsonsErr,function(index, item){ if(item.status == "OK"){ $(".BDALL").append( '<div class="BDSS">'+ '<div class="BDS_state" data-Text="正常">'+'</div>'+ '<div class="BDS_stateA">'+ '<P class="BDS_ID">'+'ID:'+ item.cardid +'</P>'+ '<P class="BDS_time">'+'时间:'+item.checkTime+'</P>'+ '<div class="BDS_stateWhy">'+ '<P class="BDS_decser">'+'备注:'+'</P>'+ '<p class="BDS_decserTexts" >'+ "无" +'</P>'+ '</div>'+ '</div>'+ '</div>'); }else if(item.status == "NG"){ $(".BDALL").append( '<div class="BDSS">'+ '<div class="BDS_states" data-Text="异常">'+'</div>'+ '<div class="BDS_stateA">'+ '<P class="BDS_ID">'+'ID:'+ item.cardid +'</P>'+ '<P class="BDS_time">'+'时间:'+item.checkTime+'</P>'+ '<div class="BDS_stateWhy">'+ '<P class="BDS_decser">'+'备注:'+'</P>'+ '<p class="BDS_decserTexts" >'+ item.msg +'</P>'+ '</div>'+ '</div>'+ '</div>'); } }); //关键词搜索 // 实时搜索功能 $('#searchInput').on('input', function() { const keyword = $(this).val().trim(); const $pTags = $('.BDSS p'); let matchCount = 0; let firstMatch = null; // 清除之前的高亮 $pTags.removeClass('highlight'); $pTags.find('span.highlight').each(function() { $(this).replaceWith($(this).text()); }); if (keyword.length > 0) { // 遍历所有p标签 $pTags.each(function() { const $p = $(this); const text = $p.text(); const regex = new RegExp(keyword, 'gi'); if (regex.test(text)) { matchCount++; // 标记第一个匹配项 if (!firstMatch) firstMatch = this; // 高亮显示关键词 const highlighted = text.replace(regex, match => `<span class="highlight">${match}</span>` ); $p.html(highlighted); } }); // 显示匹配数量 $('#resultCount').text(`找到 ${matchCount} 个结果`); // 滚动到第一个匹配项 if (firstMatch) { $('.BDALL').animate({ scrollTop: $(firstMatch).offset().top - $('.BDALL').offset().top + $('.BDALL').scrollTop() - 20 }, 500); } } else { $('#resultCount').text(''); // 恢复原始文本 $pTags.each(function() { $(this).text($(this).text()); }); } });</script>,想实现u4Scoll根据fliale所在的位置高度作为u4Scoll的盒子高度,BDALL里面新增的数据一旦超过u4Scoll盒子高度就开始overflow-y触发。由于在不同屏幕上fliale所在的位置高度也不一样,所以得让u4Scoll盒子的高度跟fliale所在的位置高度同步才行。怎么实现这个功能(这个代码实现地方是不同类型的android手机的移动端的屏幕)
最新发布
08-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值