模拟百度分享侧边栏的弹出与滑入效果。当鼠标移入#div1分享侧边栏,#div1分享侧边栏区块匀速滑出直至其全部露出。当鼠标移除#div1分享侧边栏,#div1分享侧边栏区块匀速滑入隐藏,直至恢复初始位置。若#div1区块未全部露出时,鼠标移出,#div1区块则开始滑入隐藏;若#div1区块未全部滑入隐藏,鼠标移入,则div1区块则开始匀速弹出。

<!DOCTYPE html>
<html> <head>
<title>JS运动框架案例:类百度分享制作</title>
<meta charset="UTF-8">
</head>
<style>
#div1 {
width: 150px;
height: 200px;
background: green;
position: absolute;
left: -150px;
top: calc(50% - 100px);
} #div1 span {
width: 20px;
height: 60px;
line-height: 20px;
right: -20px;
top: 70px;
background: blue;
position: absolute; } html,
body {
margin: 0;
padding: 0;
}
</style>
<script> // 补充代码 </script> <body> <div id='div1'>
<span>分享到</span>
</div>
</body> </html>

参考代码:

参考代码(1)以目标点作为参数,如果目标点为0,速度为正,目标点为-150,速度为负。如果运动抵达目标点,那么关闭定时器;否则left值发生变化。

function startMove(iTarget)
{
var oDiv=document.getElementById('div1'); clearInterval(timer);
timer=setInterval(function (){
var speed=0; if(oDiv.offsetLeft>iTarget)
{
speed=-10;
}
else
{
speed=10;
} if(oDiv.offsetLeft==iTarget)
{
clearInterval(timer);
}
else
{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
}, 30);
}

参考代码(2)以每次运动的距离作为参数,鼠标移入参数为正,鼠标移除参数为负。

    window.onload = function () {
oDiv = document.getElementById('div1');
function startMove(speed) {
var iTarget = null;
clearInterval(oDiv.timer); oDiv.timer = setInterval(function () { if (speed > 0) { iTarget = 0; } else {
iTarget = -150;
} var l=Math.abs(iTarget-oDiv.offsetLeft);
var s=Math.abs(speed); if (l<=s) {
oDiv.style.left=iTarget+'px';
clearInterval(oDiv.timer); } else { oDiv.style.left = oDiv.offsetLeft + speed + 'px';
} }, 30); } oDiv.onmouseover = function () {
startMove(10)
}
oDiv.onmouseout = function () {
startMove(-10) } }

debug版本:为什么采用如下的代码,在弹出时,最终会多出10px距离?

    window.onload = function () {
oDiv = document.getElementById('div1');
function startMove(speed) { var iTarget = null;
clearInterval(oDiv.timer); oDiv.timer = setInterval(function () {
if (speed > 0) { iTarget = 0; } else {
iTarget = -150;
}
//bug:为什么在弹出时,会多出来10px?
if (speed > 0 && iTarget - oDiv.offsetLeft <= speed) { oDiv.style.left = iTarget + 'px'
clearInterval(oDiv.timer); } else { oDiv.style.left = oDiv.offsetLeft + speed + 'px';
} if (speed < 0 && iTarget - oDiv.offsetLeft >= speed) {
oDiv.style.left = iTarget + 'px'
clearInterval(oDiv.timer); } else { oDiv.style.left = oDiv.offsetLeft + speed + 'px';
} }, 30); } oDiv.onmouseover = function () {
startMove(10) }
oDiv.onmouseout = function () {
startMove(-10) } }

  debug代码:

    window.onload = function () {
oDiv = document.getElementById('div1');
function startMove(speed) { var iTarget = null;
clearInterval(oDiv.timer); oDiv.timer = setInterval(function () {
if (speed > 0) { iTarget = 0; } else {
iTarget = -150;
}
//bug:为什么在弹出时,会多出来10px?
/* 编码逻辑错误,应该采用if(){……}else if(){}语法,而不是并列的两个if(){……}else{……}。因为当判断语句【1】执行条件if满足时,同样满足判断语句【2】的else语句代码,导致多出来10px。
*
*/ // 当鼠标移入时,判断语句【1】,如果speed大于0且与目标点距离不大于speed时, oDiv.style.left直接设置为目标点并关闭定时器,否则继续运动。
if (speed > 0 && iTarget - oDiv.offsetLeft <= speed) { oDiv.style.left = iTarget + 'px'; clearInterval(oDiv.timer); } else { oDiv.style.left = oDiv.offsetLeft + speed + 'px';
}
// 当鼠标移出时, 判断语句【2】,如果speed小于0且与目标点距离不大于speed时, oDiv.style.left直接设置为目标点并关闭定时器,否则继续运动。
if (speed < 0 && iTarget - oDiv.offsetLeft >= speed) { oDiv.style.left = iTarget + 'px'; clearInterval(oDiv.timer); } else { oDiv.style.left = oDiv.offsetLeft + speed + 'px';
} }, 30); } oDiv.onmouseover = function () {
startMove(10) }
oDiv.onmouseout = function () {
startMove(-10) } }

  

JS模拟百度分享侧边栏效果的更多相关文章

  1. CSS实现模拟百度分享侧边栏效果

    在<JS模拟百度分享侧边栏效果>一文中对于Div区块的运动通过JS实现了鼠标移入滑出显示,鼠标移出滑入隐藏的效果.其实在CSS3中通过transition属性就可以较为轻松实现. < ...

  2. 前端 ---- js 模拟百度导航栏滚动案例

    模拟百度导航栏滚动监听 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  3. Vue.js模拟百度下拉框

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. js 模拟百度关键字搜索与跳转

    测试效果: css样式: ul{ display:none; } html代码: <input type="text" id="text" /> & ...

  5. 用于模拟百度分享的errno错误代码

    0:成功;-1:由于您分享了违反相关法律法规的文件,分享功能已被禁用,之前分享出去的文件不受影响.;-2:用户不存在;请刷新页面后重试;-3:文件不存在;请刷新页面后重试;-4:登录信息有误,请重新登 ...

  6. js模拟浏览器加载效果 pace.js 中文官方文档

    2017年2月20日12:11:25 官网URL:http://github.hubspot.com/pace/docs/welcome/ 文档 http://github.hubspot.com/p ...

  7. 原生JS模拟百度搜索关键字与跳转

    <style type="text/css"> *{ margin: 0; padding: 0; } #text{ width: 300px; height: 30p ...

  8. JS——模拟百度搜索

    注意事项: 1.for循环移除子节点时,其长度是变化的 2.在文档流中,input.img.p等标签与其他标签有3px的距离,利用左浮动,可以消除3px距离 3.背景图片定位时,第一个值是x轴方向的值 ...

  9. 第一百四十六节,JavaScript,百度分享保持居中--下拉菜单

    JavaScript,百度分享保持居中--下拉菜单 百度分享保持居中 效果图 html代码 <div id="share"> <h2>分享到</h2& ...

随机推荐

  1. JAVA对象头详解(含32位虚拟机与64位虚拟机)

    为什么要学习Java对象头 学习Java对象头主要是为了解synchronized底层原理,synchronized锁升级过程,Java并发编程等. JAVA对象头 由于Java面向对象的思想,在JV ...

  2. JVM(五):JVM模型与GC

    确定垃圾 引用计数(存在循环引用问题) 根可达算法 常见的垃圾回收算法 标记清除算法-位置不连续,产生碎片 拷贝算法- 没有碎片,浪费空间 标记压缩-没有碎片,效率偏低(多线程需要进行线程同步,单线程 ...

  3. 【编程学习笔记】如何组织构建多文件 C 语言程序!编程也有~

    优秀 Unix 程序哲学 首先,你要知道这个 C 程序是一个 Unix 命令行工具.这意味着它运行在(或者可被移植到)那些提供 Unix C 运行环境的操作系统中.当贝尔实验室发明 Unix 后,它从 ...

  4. PostgreSQL查询表名称及表结构

    1. 查询表名称 在psql状态下查询表名称 \dt SQL方式查看表名称 SELECT viewname FROM pg_views WHERE schemaname ='public' Postg ...

  5. spring boot:redis+lua实现顺序自增的唯一id发号器(spring boot 2.3.1)

    一,为什么需要生成唯一id(发号器)? 1,在分布式和微服务系统中, 生成唯一id相对困难, 常用的方式: uuid不具备可读性,作为主键存储时性能也不够好, mysql的主键,在分库时使用不够方便, ...

  6. ansible用user/group模块管理受控机上的用户和组(ansible2.9.5)

    一,ansible的user/group模块的用途: ansible的user模块用来实现:生成用户.删除用户等用户的管理ansible的group模块用来实现:生成组.删除组等组的管理 说明:刘宏缔 ...

  7. scrapy数据写入管道

    1 setting里面启动管道 ITEM_PIPELINES = { 'ganji.pipelines.GanjiPipeline': 300,}2 拿到的数据通过yield返回给管道 # -*- c ...

  8. 本地ssh快速登录 ssh免密登录

    每次登录都要ssh -p wang@xx.xx.xx.xx 虽然做了公钥验证 https://www.cnblogs.com/php-linux/p/10795913.html 不需要输入密码,但是每 ...

  9. cgdb安装

    cgdb官网:http://cgdb.github.io/ 一.cgdb安装 可使用wget命令下载,wget  http://cgdb.me/files/cgdb-0.7.0.tar.gz 之后解压 ...

  10. JavaWeb中的域对象

    域对象:可以在不同Servlet之间传递数据的对象,就是域对象,它们内置了map集合,都有setAttribute和getAttribute方法: Java Web有ServletContext.Se ...