简单弹窗拖拽

<!doctype html>
<!--<!DOCTYPE> 声明位于文档中的最前面的位置,处于 <html> 标签之前。此标签可告知浏览器文档使用哪种 HTML 或 XHTML 规范。-->
<html>
    <head>
        <!--声明当前页面的编码集:charset=gbk,gb2312(中文编码),utf-8国际编码-->
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <script type="text/javascript" src="引入jquery "></script><!--引入的js  src是引入的js样式路径-->
        <link rel="stylesheet" href="" /><!--引入的css页面的样式 href是引入的css样式路径-->
        <!--当前页面的三要素-->
        <title>html模板</title>
        <meta name="Keywords" content="关键词,关键词">
        <meta name="description" content="">

<!--css,js-->
        <style type="text/css">
            *{margin:0;padding:0}
            body{font-family:"微软雅黑";}
            /*弹出框*/
            #login {width:600px;height:350px;background:#b30;box-shadow:0 0 10px #000;z-index:10;position:absolute;display:none;}
            #login #title{background:#FF8502;width:100%;height:50px;font-size:18px;line-height:50px;text-align:left;text-indent:2em;cursor:move;}
            #login #title a{text-decoration:none;display:block;float:right;
            margin-right:16px;}
            #login #title a:hover{color:#F08}
            #content{width:305px;height:175px;margin:50px auto;font-size:16px;}
            p{text-indent:0em;}
            #content p input{height:35px;width:260px;margin-left:6px;border-radius:4px;text-indent:0em;}
            #content .con input{width:10px;height:10px;margin-left: 40px;}
            #content .con a{text-decoration:none;display:block;float:right;
            margin-right:16px;color:#fff;}
            #content .wfsubmit input{margin-left:40px;border:1px solid #46A91C ;background:#46A91C;color:#fff;}
            
            /*遮罩*/
            #bg{background:#444;width:100%;height:100%;position:absolute;top:0;left:0;z-index:1;opacity:0.6;display:none;}
        </style>
    </head>
<body>
<div id="login">
    <div id="title">用户登录 <a href="#" class="tcclose">?</a></div>
    <form action="" method="post">
    <div id="content">
        <p>账号<input type="text"/></p>
        <br>
        <p>密码<input type="text"/></p>
        <br>
        <p class="con"><input type="checkbox">下次自动登录 <a href="#">忘记密码</a></p>
        <br>
        <p class="wfsubmit"><input type="button" value="立即登录"/></p>
    </div>
</div>
<div id="bg"></div>
<input type="button" onclick="tzdelog()" value="弹窗"/>
<script type="text/javascript">
function wf_tcdelog(){
        $("#login").show();
        $("#bg").show();
        var _left =($(window).width()-$("#login").width())/2;//获取左边距离
        var _top =($(window).height()-$("#login").height())/2-50;//获取左边距离
        $("#login").css({left:_left,top:_top});
}
//当浏览器进行改变的时候触发的事件
$(window).resize(function(){
        wf_tcdelog();
});

//关闭窗口
$(".tcclose").click(function(){
        $("#login").hide();
        $("#bg").hide();
});

//拖拽方法。
window.onload=function(){
    init(document.getElementById("title"));
}
var l = 0,t=0,x=0,y=0;
var isOver = false;
var zindex = 999;
function init(titleDom){
    //tm_center(divObj);//居中事件
    /*第一种*/
    var thisDom = titleDom;//获取当前title对象
    var parentDom = thisDom.parentNode;//获取当前title对应的div
    titleDom.onmousedown = function(event){//1111开始
        var e = event || window.event;//为了兼容ie和火狐
        x = e.clientX;//鼠标所在的x坐标
        y = e.clientY;//鼠标所在的y坐标
        l = parseInt(parentDom.style.left);//距离浏览器左边的位置left
        t = parseInt(parentDom.style.top);//距离浏览器顶部的位置top
        isOver = true;//定义拖动标识,防止卡顿
        zindex++;
        parentDom.style.zIndex = zindex;
        document.onmousemove = function(event){
            if(isOver){
                var e = event || window.event;//为了兼容ie和火狐
                var newLeft = l + e.clientX - x;//新的左边距
                var newTop = t + e.clientY - y;//新的顶部边距
                parentDom.style.left = newLeft+"px";
                parentDom.style.top = newTop+"px";
            }
        }; //鼠标移动的事件
        document.onmouseup = function(event){
            if(isOver){
                isOver = false;//还原标识
            }
        };//鼠标松开的事件

};///结束
};
</script>
</body>
</html>

注意:使用的时候导入jquery

时间: 2024-08-26 10:40:59

简单弹窗拖拽的相关文章

原生弹窗拖拽代码demo+简单的抽奖

拖拽效果 效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>弹窗拖拽</title> <style> *{margin:0;padding:0;} .box{position: absolute;width: 400px;height: 300px;top:100px;left:

4, 简单的拖拽效果

class Main extends egret.DisplayObjectContainer { /** * 入口文件, 最先执行的构造方法 * 这会实例化一个和手机屏幕一样大的舞台 */ public constructor() { super(); this.once( egret.Event.ADDED_TO_STAGE, this.begin, this ); } /** * 入口文件加载成功后执行的方法 * 也是逻辑的开始, 通过触摸移动显示对象 */ private begin(e

弹窗拖拽组件开发应用

需要注意的问题包括: 1.this的指向到底是指向谁--弄清楚所指的对象 2.深入理解原型的概念及使用: 去改写对象下面公用的方法或者属性 , 让公用的方法或者属性在内存中存在一份 ( 提高性能) 1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <

jquery监听事件on写法以及简单的拖拽效果

引子——关于jquery的某些写法 我先不对监听事件做解释,我们先来看下jquery的一些写法吧!我们最常用的是jquery的css()方法,相信大家都会用! 假如用css设置一个属性,我们写法如下: $("#haorooms").css("width","100px"); 假如多个属性呢?我们写法如下: $(".demo").css({"height":"100px","ba

WEB前端开发学习----11. JQuery 实现简单的拖拽效果

拖拽效果在网页中很常见.实现起来也不难.记录一下今天实现的简单效果. 拖拽演示 快速拖动时,会出现问题,以后修改. 说白了,就是3个点击事件. 1. 按下鼠标左键, 触发点击事件,此时记录下可以得到鼠标相对于拖动控件的位置(当前鼠标位置-控件位置): 2. 拖动鼠标,触发移动事件,可以计算出鼠标移动的距离(当前鼠标位置-之前算出的相对位置),也就是拖拽控件所移动的距离: 3. 鼠标抬起,结束拖动. 在JQ中,event.pageX    event.pageY可以获取鼠标的位置,相对于文档左上角

WinForm实现简单的拖拽功能(C#)

用到了ListBox和TreeView两个控件,ListBox作为数据源,通过拖拽其中的数据放置到TreeView上,自动添加一个树节点 ListBox控件的MouseDown用于获取要拖拽的值并调用DoDragDrop方法 private void listBox1_MouseDown(object sender, MouseEventArgs e)        {            //调用DoDragDrop方法            if (this.listBox1.Select

javascript实现最简单的拖拽效果

// by zhangxinxu welcome to visit my personal website http://www.zhangxinxu.com/ // zxx.drag v1.0 2010-03-23 元素的拖拽实现 var params = { left: 0, top: 0, currentX: 0, currentY: 0, flag: false }; //获取相关CSS属性 var getCss = function(o,key){ return o.currentSt

js 实现简单的拖拽

步骤 使用 JavaScript 实现拖拽的步骤: 让元素捕获事件(mousedown, mousemove & mouseup) 单击并不释放,触发 mousedown,标记开始拖拽,并获取元素和鼠标的位置 拖动鼠标,触发 mousemove,不断的获取鼠标的位置,并通过计算重新确定元素的位置 释放师表,触发 mouseup,结束拖拽,确定元素位置并更新 代码1: demo1.html <!DOCTYPE html><html lang="en">&l

面向对象实战之封装拖拽对象

面向对象实战之封装拖拽对象 利用前面几章的所涉及到的知识,封装一个拖拽对象.为了能够帮助大家了解更多的方式与进行对比,我会使用三种不同的方式来实现拖拽. 不封装对象直接实现: 利用原生JavaScript封装拖拽对象: 通过扩展jQuery来实现拖拽对象. 本文的例子会放置于codepen.io中,供大家在阅读时直接查看.如果对于codepen不了解的同学,可以花点时间稍微了解一下. 拖拽的实现过程会涉及到非常多的实用小知识,因此为了巩固我自己的知识积累,也为了大家能够学到更多的知识,我会尽量详