愚墨的博客
  • 首页
  • 前端技术
  • 面试
只争朝夕不负韶华
  1. 首页
  2. 前端技术
  3. 正文

js之侧栏分享

2016年01月07日 2889点热度 0人点赞 0条评论

好多网站都有这个功能,其实就是个小东西,很简单。这个是最简单的,自己瞎做的,练习练习。

css代码

#div1{ width:150px; height:200px; background:#40ADE9; position:absolute;left:-150px; top: 50px;}
#div1 span{ position:absolute; height:60px; width:20px; line-height:20px; right:-20px;top:70px; background:#E55DED;}

js代码

window.onload=function()
{
	var oDiv=document.getElementById('div1');
	oDiv.onmouseover=function()
	{
		startmove(0)  //出来的left的指定位置
	};
	oDiv.onmouseout=function()
	{
		startmove(-150)//回去的时候left的指定位置
	};
};
	var timer=null;
	function startmove(target)    //先写一个运动框架
	{
		var oDiv=document.getElementById('div1');
		var speed=0;
		if(oDiv.offsetLeft>target)  // 判断一下速度的取值,出和进的时候值不同
		{
			speed=-10;
		}else{
			speed=10;
		}
		clearInterval(timer);		//为了不出错,设置定时器之前先清除所有定时器
		timer=setInterval(function(){ //设置定时器
			if(oDiv.offsetLeft==target)  //如果到达预定位置,关闭定时器
			{
				clearInterval(timer);
			}else{						//如果没有到达位置,用left属性,将其移动到指定位置
				oDiv.style.left=oDiv.offsetLeft+speed+'px';
			}
		},30)	
	}

html代码


分享到
标签: 暂无
最后更新:2016年07月17日

愚墨

保持饥渴的专注,追求最佳的品质

点赞
< 上一篇
下一篇 >

文章评论

取消回复

搜搜看看
历史遗迹
  • 2023年5月
  • 2022年9月
  • 2022年3月
  • 2022年2月
  • 2021年12月
  • 2021年8月
  • 2021年7月
  • 2021年5月
  • 2021年4月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年9月
  • 2020年7月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年1月
  • 2019年5月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年9月
  • 2018年3月
  • 2018年2月
  • 2018年1月
  • 2017年11月
  • 2017年7月
  • 2017年6月
  • 2017年3月
  • 2017年2月
  • 2017年1月
  • 2016年12月
  • 2016年11月
  • 2016年9月
  • 2016年8月
  • 2016年7月
  • 2016年6月
  • 2016年5月
  • 2016年4月
  • 2016年3月
  • 2016年2月
  • 2016年1月
  • 2015年12月
  • 2015年10月
  • 2015年9月
  • 2015年7月
  • 2015年6月
  • 2015年4月

COPYRIGHT © 2020 愚墨的博客. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS