使用DIV窗体来动态显示内容的原理:首先採用CSS和HTML隐藏弹窗中的内容,然后利用JavaScript(本教程中是JQuery)来动态显示它们。这样的效果不仅可以充分利用有限的版面空间,并且可以提高用户体验;更重要的是,它并不影响SEO效果(由于它实际存在于页面中。仅仅是初始为不可见状态)

1、在html页面中定义一个div,并在div实现我们须要展示的内容。

  <body>
<div id="login">
<h2><img src="images/close.png" alt="" class="close" />站点登录</h2>
<form id="loginForm" >
<div class="info"></div>
<div class="user">帐 号:<input type="text" name="user" class="text" /></div>
<div class="pass">密 码:<input type="password" name="pass" class="text" /></div>
<div class="button"><input type="button" name="sub" class="submit" value="" /></div>
</form>
<div class="other">注冊新用户 | 忘记密码?</div>
</div>
</body>

一图抵千言。让我们看看这个DIV弹出窗体的效果截图:

2、我所用的CSS样式

#login {
width:350px;
height:250px;
border:1px solid #ccc;
position:absolute;
display:block;
z-index:9999;
background:#fff;
}
#login h2 {
height:40px;
line-height:40px;
text-align:center;
font-size:14px;
letter-spacing:1px;
color:#666;
background:url(images/login_header.png) repeat-x;
margin:0;
padding:0;
border-bottom:1px solid #ccc;
cursor:move;
}
#login h2 img {
float:right;
position:relative;
top:14px;
right:8px;
cursor:pointer;
}
#login div.info {
padding:10px 0 5px 0;
text-align:center;
color:maroon;
}
#login div.user, #login div.pass {
font-size:14px;
color:#666;
padding:5px 0;
text-align:center;
}
#login input.text {
width:200px;
height:25px;
border:1px solid #ccc;
background:#fff;
font-size:14px;
}
#login .button {
text-align:center;
padding:15px 0;
}
#login input.submit {
width:107px;
height:30px;
background:url(images/login_button.png) no-repeat;
border:none;
cursor:pointer;
}
#login .other {
text-align:right;
padding:15px 10px;
color:#666;
}

这里面主要注意的是关于div样式的定义,由于须要居中展示我们使用绝对定位position:absolute;其次由于是弹出层,div必须在最外围,所以通常把z-index设置的很大,这里我们设置为z-index:9999;另一点是关于div本身是隐藏的须要设置为display:none,但这里我们须要直接看效果所以直接让它展现使用display:block;

3、我们须要让它居中展示。那么首先就必须获取浏览器的高度和宽度,假设有滚动栏的水平或者竖向偏移,还须要获取那个长度。通过计算获取div应该浏览器的位置。

$(document).ready(function()
{
jQuery.fn.extend({
center:function(width,height)
{
return $(this).css("left", ($(window).width()-width)/2+$(window).scrollLeft()).
css("top", ($(window).height()-height)/2+$(window).scrollTop()).
css("width",width).
css("height",height);
}
}); });

通过点击button让它展现

	$(".login").click(function ()
{
$("#login").show().center(350,250);//展现登陆框
});

效果图

4、能对弹出框进行拖拽

代码实现

$(document).ready(function()
{
jQuery.fn.extend({
//拖拽功能
drag:function(){
var $tar = $(this);
return $(this).mousedown(function(e){
if(e.target.tagName =="H2"){
var diffX = e.clientX - $tar.offset().left;
var diffY = e.clientY - $tar.offset().top;
$(document).mousemove(function(e){
var left = e.clientX - diffX;
var top = e.clientY - diffY;
if (left < 0){
left = 0;
}
else if (left <= $(window).scrollLeft()){
left = $(window).scrollLeft();
}
else if (left > $(window).width() +$(window).scrollLeft() - $tar.width()){
left = $(window).width() +$(window).scrollLeft() -$tar.width();
}
if (top < 0){
top = 0;
}
else if (top <= $(window).scrollTop()){
top = $(window).scrollTop();
}
else if (top > $(window).height() +$(window).scrollTop() - $tar.height()){
top = $(window).height() +$(window).scrollTop() - $tar.height();
}
$tar.css("left",left + 'px').css("top",top + 'px');
});
}
$(document).mouseup(function(){
$(this).unbind("mousemove");
$(this).unbind("mouseup")
});
});
}
}); });

这里我们仅仅针对div内容中的H2元素可供点击拖拽。假设须要全局div可进行改动,拖拽原理:当鼠标在指定元素上的按下时。获取该鼠标点坐标,通过计算,把图片也移动到相相应的位置,一旦鼠标点击取消,相相应的按下事件也随之取消,页面精巧。

调用拖拽方法

$("#login").drag();

如今我们能够点击弹出框的标题栏任意对其在浏览器中拖拽了。

影响地址

点击 登录 要么 注册

HTML中心在页面上弹出自定义表单层(实现可能拖累)的更多相关文章

  1. 实现android手机来电拦截系统页面弹出自定义页面特效

    如何实现android手机来电拦截系统页面弹出自定义页面特效, 首先:    我们需要注册一个监听来电的广播PhoneStateReceiver 类:其次:    在onReceive里面我们获取an ...

  2. ArcGIS api fo silverlight学习三(利用ElementLayer实现鼠标悬浮弹出自定义窗体)

    接着上一节继续学习,本节主要是利用ElementLayer实现鼠标悬浮弹出自定义窗体 参考博文:http://www.cnblogs.com/luxiaoxun/p/3322218.html 一.新建 ...

  3. android高德地图网络路径实现自定义marker并点击弹出自定义窗口

    android中使用地图的地方随处可见,今天记录一下网络路径生成自定义marker,点击标记弹出自定义的窗口(在这里使用的是高德地图) 在这里我们使用Grilde去加载网络图片,因为这个简直太方便了! ...

  4. 【WPF】右下角弹出自定义通知样式(Notification)——简单教程

    原文:[WPF]右下角弹出自定义通知样式(Notification)--简单教程 1.先看效果 2.实现 1.主界面是MainWindow 上面就只摆放一个Button即可.在Button的点击事件中 ...

  5. 使用 JavaScript 用循环嵌套输出乘法表。外循环控制行数,内循环控制当前行要输出的乘法表达式,在页面上输出九九乘法表

    查看本章节 查看作业目录 需求说明: 在页面上输出九九乘法表,实现效果如图所示 实现思路: 创建HTML页面 在页面中嵌入 <script type="text/javascript& ...

  6. HTML页面弹出自定义对话框带遮蔽罩(使用JavaScript)

    转载:http://blog.sina.com.cn/s/blog_610f47c50100ohe4.html 原理其实很简单:首先绘制弹出的自定义对话框,将其使用display:none隐藏,因为设 ...

  7. Excel 去掉每次打开弹出自定义项安装的弹窗

    弹窗: 解决方案: 一.打开“文件”——“选项”如图. 二.选择“加载项”,下面的“管理”,选择“COM加载项”,然后点击“转到”,弹出框: 三:在“可用加载项”下面你会发现有一项是“LoadTest ...

  8. wpf之Popup弹出自定义输入"键盘"

    在很多工厂的信息化MES系统中,车间的采集数据的机器是触摸屏电脑(工厂环境所限,用外接鼠标键盘反而不方便). 由于没有外接键盘,所以用户无法像坐在办公室一样,用鼠标键盘进行录入信息. 这时我们可以用w ...

  9. Bootstrap Popover(弹出框)弹出自定义格式代码

    HEAD 标签之间引入CSS:<link href="../../../public/css/bootstrap.min.css" rel="stylesheet& ...

随机推荐

  1. MFC消息映射的原理:笔记

    多态的实现机制有两种,一是通过查找绝对位置表,二是查找名称表:两者各有优缺点,那么为什么mfc的消息映射采用了第二种方法,而不是c++使用的第一种呢?因为在mfc的gui类库是一个庞大的继承体系,而里 ...

  2. 测试关闭mojo utf-8

    [root@wx03 ~]# cat test.pl use Mojolicious::Lite; use JSON qw/encode_json decode_json/; use Encode; ...

  3. INFORMIX 时间函数大全

    http://publib.boulder.ibm.com/infocenter/idshelp/v115/index.jsp?topic=/com.ibm.sqls.doc/ids_sqs_0187 ...

  4. Swift - 自定义函数规则说明

    1,无返回值的函数 1 2 3 func test(name:String){   } 2,返回一个返回值 1 2 3 func test(name:String) -> Bool{     r ...

  5. go(一)变量

    package main import ( "fmt" ) func main() { var a int a = var a1 string a1 = "my is a ...

  6. 100M 宽带办理

    http://zj.189.cn/zhuanti/kdsbz#%E5%8D%95%E5%AE%BD%E5%B8%A6%E7%89%B9%E6%83%A0

  7. UITableView性能优化及手工绘制UITableViewCell

    提高表视图的性能 UITableView作为应用中最常用的视图,它的性能优化问题几乎是经常提及.下面对在非网络访问情况下的表视图性能优化进行了主要的几点说明: 1.自定义类或XIB文件时 在系统提供的 ...

  8. POJ 1159 - Palindrome 优化空间LCS

    将原串和其逆序串的最长公共子序列求出来为M..那么2*n-M就是所需要加的最少字符..因为求出的M就是指的原串中"潜伏"的最长回文.. 问题转化为求LCS..但是n最大到5000. ...

  9. java中Executor、ExecutorService、ThreadPoolExecutor介绍(转)

    1.Excutor 源码非常简单,只有一个execute(Runnable command)回调接口 public interface Executor { /**     * Executes th ...

  10. 什么是Java “实例化”

    实例化:对象也是引用数据类型,只能使用new运算符从堆中分配内存: 使用已经定义好的类,创建该类对象的过程称为“实例化”. 只有先实例化类的对象,才可以访问到类中的成员(属性和方法). 使用成员运算符 ...