一: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>

  效果:

  

  

  

最新文章

  1. 。net用lamda实现属性的优雅操作
  2. CircularSeekBar
  3. 优化SQLServer&mdash;&mdash;表和分区索引(二)
  4. 使用 Sublime Text 做 Javascript 编辑器 - 集成 SublimeCodeIntel 实现代码智能提示及自动完成
  5. 3.求m+mm+mmm+…+m…m(n个)的和,其中m为1~9之间的整数。 例如,当m=3、n=4时,求3+33+333+3333的和。
  6. [转载]EF Code First 学习笔记:约定配置
  7. C# 委托总结
  8. ural 1998 The old Padawan
  9. 【WCF】错误处理(一):FaultException 与 FaultReason 的搭配
  10. Google会思考的深度学习系统
  11. 网络Socket编程及实例
  12. 使用cloudreve搭建个人网盘
  13. 三、CSS样式——字体
  14. node中的cluster模块开启进程,进程共享数据
  15. Spark入门到精通--(第一节)Spark的前世今生
  16. CUDA driver version is insufficient for CUDA runtime version 解决
  17. Git Pull Github and Gitee or Gitlab
  18. word2013 如何设置从第三页开始编码 或 如何设置封面页和正文页页码不连续
  19. 使用postman做接口测试(三)
  20. JBoss DataGrid的集群部署与訪问

热门文章

  1. idea中添加web.xml配置文件与tomcat启动中遇到的web.xml文件找不到的问题
  2. [ipsec][strongswan] strongswan源码分析-- (二)rekey/reauth机制分析
  3. LAMP环境搭建基本步骤
  4. 关于bat文件的批处理
  5. 小程序框架之视图层 View~基础组件
  6. Servlet的入门
  7. keras模块学习之-目标函数(objectives)笔记
  8. Bias vs. Variance(1)--diagnosing bias vs. variance
  9. *.Net框架 - IGrouping类 &amp; Lookup类
  10. java EL表达式中${param.name}详细