可展开收起效果的网站提示框

本文介绍了使用JavaScript和jQuery实现网站提示滑动效果的方法。通过具体代码示例,展示了如何利用定时器和animate()方法控制元素的左移动画,实现提示框的显示与隐藏。

分别用JavaScript和jQuery写了滑动效果的网站提示。

用jQuery很简单,用 animate() 方法就可以了。

用JavaScript稍微复杂一点,需要用到定时器实现动画效果,并且要考虑到鼠标多次移入“提示”时会开启多个定时器,则要清除定时器的情况。

效果如图:

收起时:  展开时: 

HTML:

<div id="cue-hide"><span id="cue-show">小贴士 </span><p>用JavaScript和jQuery分别写滑动效果的网站提示。</p></div>

JavaScript:

/*JavaScript*/
window.onload=function(){
	var cdiv=document.getElementById("cue-hide");
	cdiv.onmouseover=function(){
		cueshow();
	};
	cdiv.onmouseout=function(){
		cuehide();
	};
}
var timer=null;
function cueshow(){
	clearInterval(timer);
	var cdiv=document.getElementById("cue-hide");
	timer=setInterval(function(){
		if(cdiv.offsetLeft==0){
			clearInterval(timer);
		}else{
			cdiv.style.left = cdiv.offsetLeft+25+'px'; 
		}
	},50);
};
function cuehide(){
	clearInterval(timer);
	var cdiv=document.getElementById("cue-hide");
	timer=setInterval(function(){
		if(cdiv.offsetLeft==-200){
			clearInterval(timer);
		}else{
			cdiv.style.left = cdiv.offsetLeft-25+'px'; 
		}
	},50);
};

jQuery:

$(document).ready(function(){
	$("#cue-hide").mouseenter(function(){
		$("#cue-hide").animate({left:"0px"},"slow"); 
	});
	$("#cue-hide").mouseleave(function(){
		$("#cue-hide").animate({left:"-200px"},"slow"); 
	});
});

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值