html弹窗可以复制,简单漂亮的js弹窗可自由拖拽且兼容大部分浏览器

这篇博客介绍了一个简单的JavaScript实现的可自由拖拽的弹窗功能。代码示例展示了如何通过事件监听和样式修改,使弹窗在页面上可以被用户随意移动,并提供了关闭和点击事件的处理,支持跳转操作。此外,还提供了相应的CSS样式以美化弹窗的外观。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单漂亮的js弹窗 - 自由拖拽

简单漂亮的js弹窗 - 自由拖拽

//作者:zhao jinhan

//时间: 2013年10月22日

//邮箱:xb_zjh@126.com

function drag(o,s)

{

//drag(obj [,scroll]);

//obj:对象的id或对象本身;

//scroll(可选):对象是否随窗口拖动而滑动,默认为否

if (typeof o == "string") o = document.getElementById(o);

o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;

o.orig_y = parseInt(o.style.top) - document.body.scrollTop;

o.orig_index = o.style.zIndex;

o.onmousedown = function(a)

{

this.style.zIndex = 10000;

var d=document;

if(!a)a=window.event;

var x = a.clientX+d.body.scrollLeft-o.offsetLeft;

var y = a.clientY+d.body.scrollTop-o.offsetTop;

d.ondragstart = "return false;"

d.onselectstart = "return false;"

d.onselect = "document.selection.empty();"

if(o.setCapture)

o.setCapture();

else if(window.captureEvents)

window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);

d.onmousemove = function(a)

{

if(!a)a=window.event;

o.style.left = a.clientX+document.body.scrollLeft-x;

o.style.top = a.clientY+document.body.scrollTop-y;

o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;

o.orig_y = parseInt(o.style.top) - document.body.scrollTop;

}

d.onmouseup = function()

{

if(o.releaseCapture)

o.releaseCapture();

else if(window.captureEvents)

window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);

d.onmousemove = null;

d.onmouseup = null;

d.ondragstart = null;

d.onselectstart = null;

d.onselect = null;

o.style.cursor = "normal";

o.style.zIndex = o.orig_index;

}

}

if (s)

{

var orig_scroll = window.onscroll?window.onscroll:function (){};

window.onscroll = function ()

{

orig_scroll();

o.style.left = o.orig_x + document.body.scrollLeft;

o.style.top = o.orig_y + document.body.scrollTop;

}

}

}

var title = '提示标题';

var message='提示内容';

var TrueEvent='';

var CancelEvent='';

var CloseEvent='';

function dialogalert(title, message, TrueEvent, CancelEvent, CloseEvent) {

$("#divBackground").removeClass("hidden");

$("#divMaincontent").removeClass("hidden");

$("#alertTitle").html(title);

$("#alertContent").html(message);

$("#btnSure").click(function () {

$("#divBackground").addClass("hidden");

$("#divMaincontent").addClass("hidden");

$("#btnSure").unbind("click");

if (TrueEvent) {

//确认之后跳转url

window.location.href = TrueEvent;

}

}); //确定

$("#btnCancel").click(function () {

$("#divBackground").addClass("hidden");

$("#divMaincontent").addClass("hidden");

$("#btnCancel").unbind("click");

if (CancelEvent) {

//取消之后跳转url

window.location.href = CancelEvent;

}

});//取消

$("#btnClose").click(function () {

$("#divBackground").addClass("hidden");

$("#divMaincontent").addClass("hidden");

$("#btnClose").unbind("click");

if (CloseEvent) {

//关闭之后跳转url

window.location.href = CloseEvent;

}

});//X关闭

}

$(function(){

drag("divMaincontent");

dialogalert("弹窗标题","弹窗信息",'http://www.baidu.com/','http://www.icode100.com/','http://bbs.icode100.com/');

});

body, p {

background: none repeat scroll 0 0 #FFFFFF;

color: #333333;

font-size: 12px;

margin: 0;

padding: 0;

}

.hidden { display:none}

.left { float:left }

.right { float:right }

#divMaincontent {

font-family: "微软雅黑","宋体",Microsoft Yahei,Tahoma,Arial,sans-serif;

font-size: 100%;

font-weight: normal;

position:absolute;

vertical-align: baseline;

}

#divMaincontent a { text-decoration:none; color:#333333;}

.blackf1 {

clear: both;

font-size: 0;

height: 12px;

line-height: 12px;

}

.fontcolorf1 {

color: #999999;

}

.pm_main {

border: 4px solid rgba(0, 0, 0, 0.1);

left: 40%;

position: fixed;

top: 40%;

width: 318px;

z-index: 10001;

}

.pm_tit1 {

background: url("images/dialog_blue.gif") repeat-x scroll 0 0 rgba(0, 0, 0, 0);

color: #FFFFFF;

height: 26px;

line-height: 26px;

padding: 0 1px;

text-indent: 16px;

width: 100%;

cursor:move;

}

.pm_tit1 a {

background: url("images/pm_bg.png") no-repeat scroll 0 -67px rgba(0, 0, 0, 0);

cursor: pointer;

height: 20px;

margin-right: 10px;

width: 20px;

}

.pm_tit1 a:hover {

background-position: 0 -90px;

}

.pm_con1 {

background: none repeat scroll 0 0 #CCCCCC;

max-height: 150px;

overflow-y: auto;

padding: 0 1px 1px;

width: 100%;

}

.pm_con2 {

background: none repeat scroll 0 0 #FFFFFF;

overflow: hidden;

padding: 16px 0;

width: 100%;

}

.pm_con2 p {

margin-left: 80px;

padding: 2px 0;

text-align: left;

}

.pm_con2 p.fontcolorf3 {

font-size: 14px;

font-weight: bold;

}

.pm_btn1 a, .pm_btn1 a:link, .pm_btn1 a:visited, .pm_btn1 a:hover, .pm_btn1 a:active {

background: url("images/pm_bg.png") no-repeat scroll 0 0 rgba(0, 0, 0, 0);

display: inline-block;

height: 28px;

line-height: 28px;

margin: 3px 40px 0 0;

text-align: center;

width: 53px;

}

.pm_btn1 a:hover {

background-position: 0 -35px;

color: #333333;

text-decoration: none;

}

效果图片:

55608d766de4ab514d8f1f59f3461975.gif 

images/里面的图片:

dialog_blue.gif:

d1a0a56e7ad440d0241bc1b3f1202b8b.gif 

pm_bg.png

032f1bd4a66c68609ef57b35b6b36323.png 

文件包下载:

//www.jb51.net/jiaoben/112059.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值