JS模拟百度分享侧边栏效果
模拟百度分享侧边栏的弹出与滑入效果。当鼠标移入#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模拟百度分享侧边栏效果的更多相关文章
- CSS实现模拟百度分享侧边栏效果
在<JS模拟百度分享侧边栏效果>一文中对于Div区块的运动通过JS实现了鼠标移入滑出显示,鼠标移出滑入隐藏的效果.其实在CSS3中通过transition属性就可以较为轻松实现. < ...
- 前端 ---- js 模拟百度导航栏滚动案例
模拟百度导航栏滚动监听 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- Vue.js模拟百度下拉框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js 模拟百度关键字搜索与跳转
测试效果: css样式: ul{ display:none; } html代码: <input type="text" id="text" /> & ...
- 用于模拟百度分享的errno错误代码
0:成功;-1:由于您分享了违反相关法律法规的文件,分享功能已被禁用,之前分享出去的文件不受影响.;-2:用户不存在;请刷新页面后重试;-3:文件不存在;请刷新页面后重试;-4:登录信息有误,请重新登 ...
- js模拟浏览器加载效果 pace.js 中文官方文档
2017年2月20日12:11:25 官网URL:http://github.hubspot.com/pace/docs/welcome/ 文档 http://github.hubspot.com/p ...
- 原生JS模拟百度搜索关键字与跳转
<style type="text/css"> *{ margin: 0; padding: 0; } #text{ width: 300px; height: 30p ...
- JS——模拟百度搜索
注意事项: 1.for循环移除子节点时,其长度是变化的 2.在文档流中,input.img.p等标签与其他标签有3px的距离,利用左浮动,可以消除3px距离 3.背景图片定位时,第一个值是x轴方向的值 ...
- 第一百四十六节,JavaScript,百度分享保持居中--下拉菜单
JavaScript,百度分享保持居中--下拉菜单 百度分享保持居中 效果图 html代码 <div id="share"> <h2>分享到</h2& ...
随机推荐
- mysql linux 命令行操作
1. 登录mysql mysql -u 用户名 -p 回车输入密码
- [论文阅读]阿里DIN深度兴趣网络之总体解读
[论文阅读]阿里DIN深度兴趣网络之总体解读 目录 [论文阅读]阿里DIN深度兴趣网络之总体解读 0x00 摘要 0x01 论文概要 1.1 概括 1.2 文章信息 1.3 核心观点 1.4 名词解释 ...
- 【Luogu】P4381 [IOI2008]Island
一.题目 Description 你将要游览一个有N个岛屿的公园.从每一个岛i出发,只建造一座桥.桥的长度以Li表示.公园内总共有N座桥.尽管每座桥由一个岛连到另一个岛,但每座桥均可以双向行走.同时, ...
- centos下安装mongodb 通过shell脚本
#! /bin/bash yum -y update echo -e "开始安装mongodb\n" download_url=https://fastdl.mongodb.o ...
- 第十四章 nginx代理配置
一.nginx代理 1.常见模式 1.常见模式:1)正向代理2)反向代理2.区别1)区别在于形式上服务的"对象"不一样2)正向代理代理的对象是客户端,为客户端服务3)反向代理代理 ...
- 【API管理 APIM】如何查看APIM中的Request与Response详细信息,如Header,Body中的参数内容
问题描述 通过APIM门户或者是Developer门户,我们可以通过Test功能测试某一个接口,通过Trace可以获取非常详细的Request,Response的信息,包含Header,X-Forwa ...
- HTML轮播(3)
前言 现在给轮播加上可视化的点,实际这样的轮播已经算完成的了 CSS #LB { width: 100%; height: 948px; overflow: hidden; position:rela ...
- UVA 12298 Super Poker II (FFT)
#include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using ...
- Docker学习—概念及基本应用
1.Doker基本概念: Docker架构: Docker使用客户端-服务器架构.Docker客户端与Docker守护进程进行对话,该守护进程完成了构建,运行和分发Docker容器的繁重工作 相关描 ...
- Docker指令整理
date: 2018-11-18 11:09:28 updated: 2018-11-18 11:09:28 Docker指令整理 管理员权限!!! 查看docker版本 docker -v 启动 s ...