011 client系列案例
一:Client系列
1.说明
clientWidth:不包括边框的可视区的宽
clientHeight:可视区的高,不包括边框
clientLeft:左边框的宽度
clientTop:上面框的宽度
2.示例-图片跟着鼠标飞
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
img {
position: absolute;
}
</style>
</head>
<body>
<img src="data:image/tianshi.gif" alt="" id="img">
<script>
//文档的鼠标移动事件
document.onmousemove=function (e) {
e=window.event||e;
document.getElementById("img").style.left=e.clientX+"px";
document.getElementById("img").style.top=e.clientY+"px";
}
</script>
</body>
</html>
但是存在一个问题,如果将heigh加大的时候,滑动鼠标,图片会上移,离开鼠标
问题:
clientY:可视区域的纵坐标
解决:
pageY: 相对于页面顶部的坐标
但是,这种在谷歌与火狐中可以使用,但是在IE8中不能使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body {
height: 2000px;
}
img {
position: absolute;
}
</style>
</head>
<body>
<img src="data:image/tianshi.gif" alt="" id="img">
<script>
//文档的鼠标移动事件
document.onmousemove=function (e) {
e=window.event||e;
document.getElementById("img").style.left=e.pageX+"px";
document.getElementById("img").style.top=e.pageY+"px";
}
</script>
</body>
</html>
继续解决:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body {
height: 2000px;
}
img {
position: absolute;
}
</style>
</head>
<body>
<img src="data:image/tianshi.gif" alt="" id="img">
<script>
//文档的鼠标移动事件
document.onmousemove=function (e) {
e=window.event||e;
document.getElementById("img").style.left=e.clientX+getscroll().left+"px";
document.getElementById("img").style.top=e.clientY+getscroll().top+"px";
} function getscroll() {
return {
top: window.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop||0,
left: window.pageXOffset||document.documentElement.scrollLeft||document.body.scrollLeft||0
};
}
</script>
</body>
</html>
效果:

二:案例
1.拖拽案例
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.login-header {
width: 100%;
text-align: center;
height: 30px;
font-size: 24px;
line-height: 30px;
} * {
padding: 0px;
margin: 0px;
} .login {
width: 512px;
position: absolute;
border: #ebebeb solid 1px;
height: 280px;
left: 50%;
right: 50%;
background: #ffffff;
box-shadow: 0px 0px 20px #ddd;
z-index: 9999;
margin-left: -250px;
margin-top: 140px;
display: none;
} .login-title {
width: 100%;
margin: 10px 0px 0px 0px;
text-align: center;
line-height: 40px;
height: 40px;
font-size: 18px;
position: relative;
cursor: move;
-moz-user-select: none; /*火狐*/
-webkit-user-select: none; /*webkit浏览器*/
-ms-user-select: none; /*IE10*/
-khtml-user-select: none; /*早期浏览器*/
user-select: none;
} .login-input-content {
margin-top: 20px;
} .login-button {
width: 50%;
margin: 30px auto 0px auto;
line-height: 40px;
font-size: 14px;
border: #ebebeb 1px solid;
text-align: center;
} .login-bg {
width: 100%;
height: 100%;
position: fixed;
top: 0px;
left: 0px;
background: #000000;
filter: alpha(opacity=30);
-moz-opacity: 0.3;
-khtml-opacity: 0.3;
opacity: 0.3;
display: none;
} a {
text-decoration: none;
color: #000000;
} .login-button a {
display: block;
} .login-input input.list-input {
float: left;
line-height: 35px;
height: 35px;
width: 350px;
border: #ebebeb 1px solid;
text-indent: 5px;
} .login-input {
overflow: hidden;
margin: 0px 0px 20px 0px;
} .login-input label {
float: left;
width: 90px;
padding-right: 10px;
text-align: right;
line-height: 35px;
height: 35px;
font-size: 14px;
} .login-title span {
position: absolute;
font-size: 12px;
right: -20px;
top: -30px;
background: #ffffff;
border: #ebebeb solid 1px;
width: 40px;
height: 40px;
border-radius: 20px;
} </style>
</head>
<body>
<div class="login-header">
<a id="link" href="javascript:void(0);">点击,弹出登录框</a>
</div>
<div id="login" class="login">
<div id="title" class="login-title">登录会员
<span>
<a id="closeBtn" href="javascript:void(0);" class="close-login">关闭</a>
</span>
</div>
<div class="login-input-content">
<div class="login-input">
<label>用户名:</label>
<input type="text" placeholder="请输入用户名" name="info[username]" id="username" class="list-input">
</div>
<div class="login-input">
<label>登录密码:</label>
<input type="password" placeholder="请输入登录密码" name="info[password]" id="password" class="list-input">
</div>
</div>
<div id="loginBtn" class="login-button"><a href="javascript:void(0);" id="login-button-submit">登录会员</a></div>
</div><!--登录框-->
<div id="bg" class="login-bg"></div><!--遮挡层--> <script> //获取超链接,注册点击事件,显示登录框和遮挡层
document.getElementById("link").onclick = function (e) {
document.getElementById("login").style.display = "block";
document.getElementById("bg").style.display = "block";
e.stopPropagation();
}; //获取关闭,注册点击事件,隐藏登录框和遮挡层
document.getElementById("closeBtn").onclick = function () {
document.getElementById("login").style.display = "none";
document.getElementById("bg").style.display = "none";
e.stopPropagation();
}; //按下鼠标,移动鼠标,移动登录框
document.getElementById("title").onmousedown = function (e) {
//获取此时的可视区域的横坐标-此时登录框距离左侧页面的横坐标
var spaceX = e.clientX - document.getElementById("login").offsetLeft;
var spaceY = e.clientY - document.getElementById("login").offsetTop;
//移动的事件
document.onmousemove = function (e) {
//新的可视区域的横坐标-spaceX====>新的值--->登录框的left属性
var x = e.clientX - spaceX+250;
var y = e.clientY - spaceY-140;
document.getElementById("login").style.left = x + "px";
document.getElementById("login").style.top = y + "px"; }
}; document.onmouseup=function () {
document.onmousemove=null;//当鼠标抬起的时候,把鼠标移动事件干掉
}; document.onclick=function () {
document.getElementById("login").style.display="none";
document.getElementById("bg").style.display = "none";
}; </script>
<!-- <script>--> <!-- // 点击超链接弹出登录框,点击页面的任何位置都可以关闭登录框-->
<!-- document.getElementById("link").onclick=function (e) {-->
<!-- document.getElementById("login").style.display="block";-->
<!-- //下面的两个是阻止事件冒泡的-->
<!-- window.event.cancelBubble=true;-->
<!-- e.stopPropagation();-->
<!-- };-->
<!-- document.onclick=function () {-->
<!-- document.getElementById("login").style.display="none";-->
<!-- console.log("隐藏了");-->
<!-- };-->
<!-- </script>--> </body>
</html>
效果:

2.程序
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>哈哈</title>
<style>
* {
margin: 0;
padding: 0;
} .box {
width: 350px;
height: 350px;
margin: 100px;
border: 1px solid #ccc;
position: relative;
} .big {
width: 400px;
height: 400px;
position: absolute;
top: 0;
left: 360px;
border: 1px solid #ccc;
overflow: hidden;
display: none;
} .mask {
width: 175px;
height: 175px;
background: rgba(255, 255, 0, 0.4);
position: absolute;
top: 0px;
left: 0px;
cursor: move;
display: none;
} .small {
position: relative;
}
</style>
</head>
<body>
<div class="box" id="box">
<div class="small"><!--小层-->
<img src="data:images/small.png" width="350" alt=""/>
<div class="mask"></div><!--遮挡层-->
</div><!--小图-->
<div class="big"><!--大层-->
<img src="data:images/big.jpg" width="800" alt=""/><!--大图-->
</div><!--大图-->
</div>
<!--导入外部的js文件-->
<script src="common.js"></script>
<script> //获取需要的元素
var box = my$("box");
//获取小图的div
var small = box.children[0];
//遮挡层
var mask = small.children[1];
//获取大图的div
var big = box.children[1];
//获取大图
var bigImg = big.children[0]; //鼠标进入显示遮挡层和大图的div
box.onmouseover = function () {
mask.style.display = "block";
big.style.display = "block";
};
//鼠标离开隐藏遮挡层和大图的div
box.onmouseout = function () {
mask.style.display = "none";
big.style.display = "none";
}; //鼠标的移动事件---鼠标是在小层上移动
small.onmousemove = function (e) {
//鼠标此时的可视区域的横坐标和纵坐标
//主要是设置鼠标在遮挡层的中间显示
var x = e.clientX - mask.offsetWidth / 2;
var y = e.clientY - mask.offsetHeight / 2;
//主要是margin的100px的问题
x = x - 100;
y = y - 100;
//横坐标的最小值
x = x < 0 ? 0 : x;
//纵坐标的最小值
y = y < 0 ? 0 : y;
//横坐标的最大值
x = x > small.offsetWidth - mask.offsetWidth ? small.offsetWidth - mask.offsetWidth : x;
//纵坐标的最大值
y = y > small.offsetHeight - mask.offsetHeight ? small.offsetHeight - mask.offsetHeight : y;
//为遮挡层的left和top赋值
mask.style.left = x + "px";
mask.style.top = y + "px"; //遮挡层的移动距离/大图的移动距离=遮挡层的最大移动距离/大图的最大移动距离
//大图的移动距离=遮挡层的移动距离*大图的最大移动距离/遮挡层的最大移动距离 //大图的横向的最大移动距离
var maxX = bigImg.offsetWidth - big.offsetWidth; //大图的纵向的最大移动距离
//var maxY = bigImg.offsetHeight - big.offsetHeight; //大图的横向移动的坐标
var bigImgMoveX = x * maxX / (small.offsetWidth - mask.offsetWidth);
//大图的纵向移动的坐标
var bigImgMoveY = y * maxX / (small.offsetWidth - mask.offsetWidth); //设置图片移动
bigImg.style.marginLeft = -bigImgMoveX + "px";
bigImg.style.marginTop = -bigImgMoveY + "px"; };
</script> </body>
</html>
效果:

011 client系列案例的更多相关文章
- 元素的属性:client系列,scroll系列,offset系
元素的属性 div.attributes 是所有标签属性构成的数组集合 dir.classList 是所有class名构成的数组集合 在classList的原型链上看一看到从 add()和remove ...
- 从零开始学 Web 之 BOM(四)client系列
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- JavaScript学习笔记5 之 计时器 & scroll、offset、client系列属性 & 图片无缝滚动
一.计时器 setInterval ( 函数/名称 , 毫秒数 )表示每经过一定的毫秒后,执行一次相应的函数(重复) setTimeout ( 函数/名称 , 毫秒数 ) 表示经过一定的毫秒后,只执行 ...
- offset系列,client系列,scroll系列回顾
一 scroll系列属性 ——滚动
- offset系列、scroll系列与client系列
offset系列: offsetLeft:获取元素距离最左边的距离,自身的margin包括在内,不包括自身的border offsetTop:获取元素距离最上边的距离,自身的margin包括在内,不包 ...
- client系列、offset系列、scroll系列
一.client系列 clientWidth/clientHeight 是我们设置的宽和高加上内边距(没有边框) clientLeft/clientTop 就是我们设置的边框值 二.offset ...
- client 系列
定义 : client翻译过来就是客户端,我们使用client系列的相关属性来获取元素可视区的相关信息.通过client系列的相关属性可以动态的得到该元素的边框大小.元素大小等.
- DDD理论学习系列——案例及目录
目录 DDD理论学习系列(1)-- 通用语言 DDD理论学习系列(2)-- 领域 DDD理论学习系列(3)-- 限界上下文 DDD理论学习系列(4)-- 领域模型 DDD理论学习系列(5)-- 统一建 ...
- Android自己定义控件系列案例【五】
案例效果: 案例分析: 在开发银行相关client的时候或者开发在线支付相关client的时候常常要求用户绑定银行卡,当中银行卡号一般须要空格分隔显示.最常见的就是每4位数以空格进行分隔.以方便用户实 ...
随机推荐
- QtCreator常用快捷键
1)帮助文件:F1 (光标在函数名字或类名上,按 F1 即可跳转到对应帮助文档,查看其详细用法) 2).h 文件和对应.cpp 文件切换:F4 3)编译并运行:Ctrl + R 4)函数声明和定义(函 ...
- redis问题解决 Caused by: io.lettuce.core.RedisException: io.lettuce.core.RedisConnectionException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specifie
1找到redis的配置文件 redis.conf vim redis.conf 修改 protected-mode yes 改为 protected-mode no 注释掉 #bin 127.0.0 ...
- windows系统开机执行文件
以下都是在windows系统下执行的 开机自启程序 新建xxx.bat的文件 编辑自己的bat文件,将所要加载的项目引导进来 注意: 如果有相对路径的话,就需要先加载项目,在用python 执行脚本程 ...
- Contest1063 - 2017广东工业大学第一次月赛-部分题解
Problem A: Chiruno Description 五年前,Aerix 无意间飞到了幻想乡,然后遇到了传说中的⑨酱,心情非常激动,想和她合影留念,但是⑨酱比较傲娇,她只欣赏算数能力强的,也不 ...
- 51nod 1657 电子龟
电子龟的行动,是沿着直线左右走动的.他能够接受两种指令,“T”(向后转,即如果面向左,改成向右:否则就向左)和“F”(向当前面朝的方向往前移动一个单位距离). 现在给出一串指令,让电子龟来执行.你必须 ...
- tensorflow 与cuda、cudnn的对应版本关系
来源:https://www.cnblogs.com/zzb-Dream-90Time/p/9688330.html
- 《你们都是魔鬼吗》实验十二 团队作业八:Alpha冲刺
<你们都是魔鬼吗>第八次团队作业:Alpha冲刺 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 你们都是魔鬼吗 作业学习目标 软件测 ...
- 十四.Protobuf3扩展
在您发布使用Protocol Buffer区的代码后,您迟早会因为业务需求变更想要“改进”Protocol Buffer的定义.如果你想让你的新Protocol Buffer向后兼容,让你的旧Prot ...
- 【安卓周记】笔记复习记录:No.2
[安卓] 1. Activity横竖屏切换生命周期变化,分三种情况: 1. 没有配置android:configChanges:每次横竖屏切换都会重新走一遍生命周期 2. 配置android:conf ...
- easyui增删改查
easyui的crud(dialog,datagrid.form讲解)1.datagrid布局2.dialog布局3.form布局4.通用的JsonBaseDao增删改方法5.dao层6.web层7. ...