js在一个div里面移动其子div
var ChildDiv = $("#cid");
var width = 0; //鼠标点击子div的地方和子div的左边边距距离
var height = 0; //鼠标点击子div的地方和子div的上边边距距离
ChildDiv.onmousedown = function (ev) //鼠标按下DIV
{
var ChildDivEvent = ev || event; //捕获点击的对象
width = ChildDivEvent.clientX - ChildDiv.offsetLeft;
height = ChildDivEvent.clientY - ChildDiv.offsetTop;
document.onmousemove = function (ev) //鼠标移动
{
var ChildDivEvent = ev || event;
var x = ChildDivEvent.clientX - width;
var y = ChildDivEvent.clientY - height;
if (x < 0) //当DIV的Left小于0,也就是移出左边
{
x = 0; //就把DIV的Left设置为0,就不能移出左边
} else if (x > $("#pid").width() - ChildDiv.offsetWidth) //DIV不能移出父DIV的右边
{
x = $("#pid").width() - ChildDiv.offsetWidth;
}
if (y < 0) //上边
{
y = 0;
} else if (y > $("#pid").height() - ChildDiv.offsetHeight) //下边
{
y = $("#pid").height() - ChildDiv.offsetHeight;
}
ChildDiv.style.left = x + 'px'; //DIV的Left设置为新鼠标X坐标减去width的值
ChildDiv.style.top = y + 'px'; //DIV的Top设置为新鼠标Y坐标减去height的值
};
document.onmouseup = function () //鼠标松开时
{
document.onmousemove = null; //把鼠标移动清除
document.onmouseup = null; //把鼠标松开清除
};
return false;
};
js在一个div里面移动其子div的更多相关文章
- 如何在一个div中使其子div居中
网上其他地方已讲述过对其的不同实现方式,今天主要做一个详细的汇总,希望对大家有帮助. ps:我面试的时候就被问到过这个问题,当时都回答错了,蓝瘦. 假设父div的类名为father,子div的类名为s ...
- 父div高度不能根据子div高度自动变化的解决方案
<div id="parent"> <div id="content"> </div> </div> 当cont ...
- css 实现 左右div 等高, 同时父级div就是最高的子div的高度
原文地址:https://www.cnblogs.com/cbza/p/7145384.html 方法一: 通过父级overflow:hidden, 自己设置padding-bottom 和 mar ...
- 父div高度不能自适应子div高度的解决方案
<div id="parent"><div id="content"> </div></div> 当conten ...
- jQuery滚动广告 解决子div绝对定位与父div重叠引起的闪烁问题
这两天做了一个滚动广告栏的demo 功能有自动轮播 左右箭头移动 导航点选中图片移动效果 模仿的是新浪体育的广告 最难的问题就是子div绝对定位于父div 鼠标移入子div 系统会判定鼠标移出了父di ...
- Position a child div relative to parent container in CSS: [设置 子DIV位置 跟 父DIV相关联]
最近调DIV的位置比较头疼,各种position: relative / absolute google到一篇好文章[http://www.webdevdoor.com/html-css/css-po ...
- CSS+DIV:父DIV相对定位+子DIV绝对定位
如何在一个div内将一个div进行绝对定位呢?很简单,把父div的position属性设为relative,子div的position属性设为absolute就可以了... 示例: <html& ...
- div鼠标悬停,子元素上移,鼠标移出,子元素下移动画。
HTML: <div class="edt_title" > <div id="edt_title"> <p class=&quo ...
- 子div设置浮动无法把父div撑开。
<div class="mainBox"> <div class="leftBox"></div> <div clas ...
随机推荐
- [CentOS7] 增加yum源
下载最新rpm文件:http://fedoraproject.org/wiki/EPEL 通过源文件rpm来增加: rpm -ivh epel-release-latest-7.noarch.rpm
- [CentOS7] at, bash, cron, anacron
声明:本文主要总结自:鸟哥的Linux私房菜-第十五章.例行性工作排程(crontab),如有侵权,请通知博主 at => /var/spool/at /etc/at.allow, /etc/a ...
- 学习笔记:首次进行JUnit+Ant构建自动的单元测试(一)
指导博客:https://blog.csdn.net/Cceking/article/details/51692010 基于软件测试的需求,使用JUnit+Ant构建自动的单元测试. IDE:ecli ...
- ORM(一)
ORM常识: 1.一对多,多的一方设置外键字段,有外键字段的表叫做子表.没有外键字段的表叫做主表. 2.主表放到子表的下面,否则子表找不到主表,写数据要先往主表中写. 数据库:(1)不创建主键,会自动 ...
- 20165224 陆艺杰 Exp 8 Web基础
.基础问题回答 (1)什么是表单 html的一个控件 表单在网页中主要负责数据采集功能 (2)浏览器可以解析运行什么语言 html xml jsp php python 等 (3)WebServer支 ...
- 利用xsltproc转换jtl报告到html报告
使用Jmeter测试完后并不能直接生成html报告,而是jtl报告.这里我们可以用xsltproc来解决. xsltproc是由DanielVeillard用来C语言编写的是一个快速XSLT引擎, ...
- before和after兼容性测试
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- LCS(Longest Common Subsequence)
http://blog.csdn.net/zztfj/article/details/6157429 LCS(Longest Common Subsequence) 就是求两个字符串最长公共子串的问题 ...
- ACM-ICPC 2018 南京赛区网络预赛 B. The writing on the wall
题目链接:https://nanti.jisuanke.com/t/30991 2000ms 262144K Feeling hungry, a cute hamster decides to o ...
- 简单的dp(dp专题)
题目链接:https://vjudge.net/contest/216347#problem/C Alice gets two sequences A and B. A easy problem ...