效果:

思路:

首先,利用计时器setInterval实现DIV的隐藏显示功能,然后在进行一个判断,之后在把要移动的相应距离进行一个参数传递,再根据参数判断出移动的方向也就是offsetLeft移动的方向,是正或者是负。最后利用onmouseover和onmouseout,实现DIV的事件。

代码:

 <head runat="server">
<title></title>
<style type="text/css">
div
{
width: 200px;
height: 300px;
background: #FF0000;
position: absolute;
left: -200px;
}
div span
{
width: 30px;
height: 90px;
background: #00FF00;
position: absolute;
right: -30px;
top: 100px;
text-align: center;
}
</style>
<script type="text/javascript">
window.onload = function () {
var oDiv1 = document.getElementById('div1');
oDiv1.onmouseover = function () {
shareMove(0);
}
oDiv1.onmouseout = function () {
shareMove(-200);
}
};
var timer = null;
function shareMove(end) {
var oDiv1 = document.getElementById('div1');
var speed = 0;
if (oDiv1.offsetLeft < end) { //根据DIV的offsetLeft距离判断DIV所要走的正负方向
speed = 10;
}
else {
speed = -10;
}
clearInterval(timer); //在一个setInterval开始之前都要先清除之前的setInterval
timer = setInterval(function () {
if (oDiv1.offsetLeft == end) { //根据参数判断DIV要走的距离
clearInterval(timer);
}
else {
oDiv1.style.left = oDiv1.offsetLeft + speed + 'px' //DIV要走的距离
}
}, 30);
}
</script>
</head>
<body>
<div id="div1">
<span>青苹果分享</span>
</div>
</body>

点滴积累【JS】---JS小功能(JS实现隐藏显示侧边栏,也就是分享栏的隐藏显示)的更多相关文章

  1. JS类小功能

    工作中,总是要处理一些前端的小功能.都是网上搜的JS脚本 <script> //防止页面后退 history.pushState(null, null, document.URL); wi ...

  2. js实现小功能 动态赋值

  3. 常用小功能js函数-函数防抖

    函数防抖:在事件被触发n秒后再执行回调,如果在这n秒内又被触发,则重新计时.这个我经常用到/** * 函数防抖 * fun 需要延时执行的函数 * delayTime 延时时间 * **/export ...

  4. js/jq 小功能函数

    1.点击复制内容到剪贴板 function copyToClip(str) { var save = function(e) { e.clipboardData.setData('text/plain ...

  5. js实现小时钟,js中Date对象的使用?

    介绍一下js中Date对象的使用 dateObj = new Date() dateObj = new Date(dateValue) dateObj = new Date(year,month,da ...

  6. js小功能整理

    /** * 判断是否包含字符串某字符串 * @param {[type]} str [被检测的字符串] * @param {[type]} substr [检测是否含有的字符串] * @return ...

  7. js小功能记录

    个人日常中遇到的js小功能记录,方便查看. /** * 判断是否包含字符串某字符串 * @param {[type]} str [被检测的字符串] * @param {[type]} substr [ ...

  8. js模仿微信语音播放的小功能

    自己写的一个模仿微信语音播放的小功能,实现的主要功能是:点击播放,点击暂停,播放切换,,,  代码如下: <!DOCTYPE html> <html lang="en&qu ...

  9. 网站开发---js与java实现的一些小功能

    记录一下网站开发过程中的一些小功能 1.js获取当前年份: <span>Copyright © 2017-<script>document.write( new Date(). ...

随机推荐

  1. Eclipse环境安装C/C++插件CDT和Eclipse安装的插件卸载(删除)

    http://blog.csdn.net/typa01_kk/article/details/49252513

  2. error C2248: 'MyString::pCharArray' : cannot access private member declared in class 'MyString'

    std::ostream & operator<<(std::ostream os,const MyString & mystr){os<<mystr.pCha ...

  3. Oracle数据库中的所有用户名

    select * from dba_users; 查看数据库里面所有用户,前提是你是有dba权限的帐号,如sys,systemselect * from all_users;  查看你能管理的所有用户 ...

  4. Postgres间隔大量写IO的解决办法

    概述 为了保证数据可靠性,同时还要保证好的读写性能,以及读写的一致性,经过多年的积累,REDO日志,shared buffer等基本成为关系型数据库的标配.postgres也不例外. 为了保证数据的可 ...

  5. 一份不太简短的LaTeX教程 lshort – A short in­tro­duc­tion to LATEX 2elshort – A short in­tro­duc­tion to LATEX 2e

    Lshort started as a trans­la­tion and ra­tio­nal­i­sa­tion of a ground-break­ing Ger­man-lan­guage i ...

  6. Binary Tree Preorder Traversal -- leetcode

    Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...

  7. unity3d的矩阵元素存储方式

    想知道u3d矩阵各个元素的存储方式,所以测试了一下 Matrix4x4 m = Matrix4x4.TRS(new Vector3(1, 2, 3), Quaternion.Euler(0, 0, 3 ...

  8. tag subshader shaderlab

    unity的黑科技 https://docs.unity3d.com/Manual/SL-SubShaderTags.html 这里 reflectCamera.RenderWithShader(re ...

  9. Maven nexus 安装nexus私服出现的两个问题

    1. 在win10中安装nexus时提示:wrapper | OpenSCManager failed - 拒绝访问. (0x5) 主要是没有权限.需要以管理员的身份运行 如果你是直接点击 start ...

  10. mac office 设置默认视图显示比例

    1.打开word 2.fn+option+F11,选中Normal,右键插入模块,复制以下脚本到编辑器中 Sub AutoOpen() ActiveWindow.ActivePane.View.Zoo ...