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位数以空格进行分隔.以方便用户实 ...
随机推荐
- sqoop 导入增量数据到hive
版本 hive:apache-hive-2.1.0 sqoop:sqoop-1.4.6 hadoop:hadoop-2.7.3 导入方式 1.append方式 2.lastmodified方式,必须要 ...
- 2013.5.3 - KDD第十五天
今天上午把昨天的想法给中秋发过去了,然后我就开始科普随机森林: 随机森林是一种比较新的机器学习模型.经典的机器学习模型是神经网络,有半个多世纪的历史了.神经网络预测精确,但是计算量很大.上世纪八十年代 ...
- egg 完整实例 增删改查MongoDB,websocket
项目地址 github.com/richard1015… 技术栈 eggjs.MongoDB.swagger.websocket.Amap 演示地址: 前台 school.zhuzhida.vip A ...
- git---怎样将分支上的一个单文件合并到主分支上(master)
一.首先切换到主分支 注意将分支上的数据全部提交 以免造成数据冲突或丢失 git checkeout master 二.选择要合并的文件 git checkout --patch 分支名称 要合并 ...
- 那些可作为GC Roots的对象
原文:https://blog.csdn.net/u010798968/article/details/72835255 一.名词解释 根搜索算法是JVM用来的判断对象是否存活的算法,此算法基本思路为 ...
- yarn cluster和yarn client模式区别——yarn-cluster适用于生产环境,结果存HDFS;而yarn-client适用于交互和调试,也就是希望快速地看到application的输出
Yarn-cluster VS Yarn-client 从广义上讲,yarn-cluster适用于生产环境:而yarn-client适用于交互和调试,也就是希望快速地看到application的输出. ...
- Kotlin构造方法详解与初始化过程分析
在上一次https://www.cnblogs.com/webor2006/p/11192025.html已经初识了Kolin的面向对象,先来回顾一下: 介绍了primary 构造方法,其特点回忆一下 ...
- python使用二分法实现在一个有序列表中查找指定的元素
二分法是一种快速查找的方法,时间复杂度低,逻辑简单易懂,总的来说就是不断的除以2除以2... 例如需要查找有序list里面的某个关键字key的位置,那么首先确认list的中位数mid,下面分为三种情况 ...
- python网络-静态Web服务器案例(29)
一.静态Web服务器案例代码static_web_server.py # coding:utf-8 # 导入socket模块 import socket # 导入正则表达式模块 import re # ...
- 神经网络(12)--具体实现:如何对back propagation的正确性进行验证
我们在进行back propagation时难免会出现各种各样的问题,当出现问题的时候,我们的cost function仍然是随着迭代的次数下降的,但是这中间会有一些问题存在,那么我们如何来检查我们的 ...