由于系统默认alert弹出窗口不能自定义样式,有可能不符合网站的风格,虽然网上应该有很多这样的JS
但是还是自己写的比较放心,自己是新手,所以顺便练习一下对DOM的操作
支持IE6下的SELECT不能遮罩的问题,谷歌支持圆角,IE6下就比较丑了,四四方方的,不过可以自定义自己喜欢的样式
听取建议后,修改了position:fixed, IE6下用hack处理了。
点击看效果:
点击模拟Alert弹出框
点击模拟Alert弹出框
点击模拟Alert弹出框
<!--
#alertMsg {
display: none;
width: 400px;
border: 1px solid #ddd;
border-radius: 5px;
box-shadow: 1px 1px 10px black;
padding: 10px;
font-size: 12px;
position: absolute;
text-align: center;
background: #fff;
z-index: 100000;
}
#alertMsg_info {
padding: 2px 15px;
line-height: 1.6em;
text-align: left;
}
#alertMsg_btn1, #alertMsg_btn2 {
display: inline-block;
background: url(http://www.zhangxinxu.com/study/image/sina_gray_btn.png) no-repeat left top;
padding-left: 3px;
color: #000000;
font-size: 12px;
text-decoration: none;
margin-right: 10px;
cursor: pointer;
}
#alertMsg_btn1 cite, #alertMsg_btn2 cite {
line-height: 24px;
display: inline-block;
padding: 0 13px 0 10px;
background: url(http://www.zhangxinxu.com/study/image/sina_gray_btn.png) no-repeat right top;
font-style: normal;
}
-->
所需CSS:
<style type="text/css">
#alertMsg
{
display
: none
;
width
: 400px
;
border
: 1px solid #ddd
;
border-radius
: 5px
;
box-shadow
: 1px 1px 10px black
;
padding
: 10px
;
font-size
: 12px
;
position
: absolute
;
text-align
: center
;
background
: #fff
;
z-index
: 100000
;
}
#alertMsg_info
{
padding
: 2px 15px
;
line-height
: 1.6em
;
text-align
: left
;
}
#alertMsg_btn1, #alertMsg_btn2
{
display
: inline-block
;
background
: url(images/gray_btn.png) no-repeat left top
;
padding-left
: 3px
;
color
: #000000
;
font-size
: 12px
;
text-decoration
: none
;
margin-right
: 10px
;
cursor
: pointer
;
}
#alertMsg_btn1 cite, #alertMsg_btn2 cite
{
line-height
: 24px
;
display
: inline-block
;
padding
: 0 13px 0 10px
;
background
: url(images/gray_btn.png) no-repeat right top
;
font-style
: normal
;
}
</style>
使用方法,直接调用函数,传递所需定义的信息,支持定义是否有取消键:
alertMsg(msg, mode)
//mode为空,即只有一个确认按钮,mode为1时有确认和取消两个按钮
点击模拟Alert弹出框
点击模拟Alert弹出框
点击模拟Alert弹出框
函数代码:添加了一个获取窗口尺寸的函数,又长长了很多,可以把获取窗口的尺寸另外立一个函数放公共库里面,这里只是为了方便演示,写到一个函数里面
function
alertMsg(msg, mode) { //
mode为空,即只有一个确认按钮,mode为1时有确认和取消两个按钮
msg = msg || '';
mode
= mode || 0;
var
isIe = (document.all) ? true
: false
;
var
isIE6 = isIe && !window.XMLHttpRequest;
var
sTop = document.documentElement.scrollTop || document.body.scrollTop;
var
sLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
var
winSize = function
(){
var
xScroll, yScroll, windowWidth, windowHeight, pageWidth, pageHeight;
//
innerHeight获取的是可视窗口的高度,IE不支持此属性
if
(window.innerHeight && window.scrollMaxY) {
xScroll
= document.body.scrollWidth;
yScroll
= window.innerHeight + window.scrollMaxY;
}
else
if
(document.body.scrollHeight > document.body.offsetHeight) { //
all but Explorer Mac
xScroll = document.body.scrollWidth;
yScroll
= document.body.scrollHeight;
}
else
{ //
Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
xScroll = document.body.offsetWidth;
yScroll
= document.body.offsetHeight;
}
if
(self.innerHeight) { //
all except Explorer
windowWidth = self.innerWidth;
windowHeight
= self.innerHeight;
}
else
if
(document.documentElement && document.documentElement.clientHeight) { //
Explorer 6 Strict Mode
windowWidth = document.documentElement.clientWidth;
windowHeight
= document.documentElement.clientHeight;
}
else
if
(document.body) { //
other Explorers
windowWidth = document.body.clientWidth;
windowHeight
= document.body.clientHeight;
}
//
for small pages with total height less then height of the viewport
if
(yScroll < windowHeight) {
pageHeight
= windowHeight;
}
else
{
pageHeight
= yScroll;
}
//
for small pages with total width less then width of the viewport
if
(xScroll < windowWidth) {
pageWidth
= windowWidth;
}
else
{
pageWidth
= xScroll;
}
return
{
'pageWidth':pageWidth,
'pageHeight':pageHeight,
'windowWidth':windowWidth,
'windowHeight':windowHeight
}
}();
//
alert(winSize.pageWidth);
//
遮罩层
var
styleStr = 'top:0;left:0;position:absolute;z-index:10000;background:#666;width:' + winSize.pageWidth + 'px;height:' + (winSize.pageHeight + 30) + 'px;';
styleStr
+= (isIe) ? "filter:alpha(opacity=80);" : "opacity:0.8;"; //
遮罩层DIV
var
shadowDiv = document.createElement('div'); //
添加阴影DIV
shadowDiv.style.cssText = styleStr; //
添加样式
shadowDiv.id = "shadowDiv";
//
如果是IE6则创建IFRAME遮罩SELECT
if
(isIE6) {
var
maskIframe = document.createElement('iframe');
maskIframe.style.cssText
= 'width:' + winSize.pageWidth + 'px;height:' + (winSize.pageHeight + 30) + 'px;position:absolute;visibility:inherit;z-index:-1;filter:alpha(opacity=0);';
maskIframe.frameborder
= 0;
maskIframe.src
= "about:blank";
shadowDiv.appendChild(maskIframe);
}
document.body.insertBefore(shadowDiv, document.body.firstChild);
//
遮罩层加入文档
//
弹出框
var
styleStr1 = 'display:block;position:fixed;_position:absolute;left:' + (winSize.windowWidth / 2 - 200) + 'px;top:' + (winSize.windowHeight / 2 - 150) + 'px;'; //
弹出框的位置
var
alertBox = document.createElement('div');
alertBox.id
= 'alertMsg';
alertBox.style.cssText
= styleStr1;
//
创建弹出框里面的内容P标签
var
alertMsg_info = document.createElement('P');
alertMsg_info.id
= 'alertMsg_info';
alertMsg_info.innerHTML
= msg;
alertBox.appendChild(alertMsg_info);
//
创建按钮
var
btn1 = document.createElement('a');
btn1.id
= 'alertMsg_btn1';
btn1.href
= 'javas' + 'cript:void(0)';
btn1.innerHTML
= '<cite>确定</cite>';
btn1.onclick
= function
() {
document.body.removeChild(alertBox);
document.body.removeChild(shadowDiv);
return
true
;
};
alertBox.appendChild(btn1);
if
(mode === 1) {
var
btn2 = document.createElement('a');
btn2.id
= 'alertMsg_btn2';
btn2.href
= 'javas' + 'cript:void(0)';
btn2.innerHTML
= '<cite>取消</cite>';
btn2.onclick
= function
() {
document.body.removeChild(alertBox);
document.body.removeChild(shadowDiv);
return
false
;
};
alertBox.appendChild(btn2);
}
document.body.appendChild(alertBox);
}
点击模拟Alert弹出框
点击模拟Alert弹出框
点击模拟Alert弹出框
分享到:
相关推荐
JavaScript弹出框组件的核心在于模拟原生的alert(), confirm() 和 prompt()函数,但提供更多的自定义选项。这些组件通常由HTML结构、CSS样式和JavaScript逻辑三部分组成。HTML负责构建弹出框的布局和元素,CSS用于...
本教程将介绍如何通过自定义CSS和JavaScript来美化`alert`和`confirm`弹出框,实现更个性化的界面效果,而无需依赖浏览器的默认样式。 首先,我们不再直接使用`window.alert()`和`window.confirm()`方法,而是创建...
总结来说,JavaScript模拟页面弹出框是一种常见的Web开发技巧,利用`iframe`的特性可以实现高度定制化的弹出交互体验。通过学习“testPopuBox”这个示例,开发者可以深入理解这一技术,并将其应用于自己的项目中,...
`alert` 函数的基本语法是 `alert(message)`,它会弹出一个只有一个按钮的对话框,用户点击后,对话框关闭,程序继续执行。`message` 参数是向用户显示的文本字符串。 `confirm` 函数的语法是 `var result = ...
要实现自定义alert,我们需要创建一个HTML和CSS结构,模拟alert对话框的外观,并使用JavaScript来控制其显示和关闭。这个过程包括以下几个步骤: 1. **HTML结构**:创建一个包含标题、内容区域和按钮的div元素,...
当我们谈论"C#前台js弹出框样式css"时,我们实际上是在讨论如何利用JavaScript来创建交互式的弹出对话框,并通过CSS(层叠样式表)来定制这些对话框的外观。下面我们将深入探讨这一主题。 首先,JavaScript的`alert...
在JavaScript编程中,`alert()`函数是一个常用的功能,它用于弹出一个带有消息的警告对话框。这个对话框通常包含一个“确定”按钮,用户必须点击后才能继续执行脚本。然而,在某些情况下,我们可能希望自定义这个...
- jQuery本身并不直接提供弹出框功能,但可以通过操作DOM元素和CSS样式来模拟弹出框效果。例如,可以创建一个隐藏的div,当需要弹出时,通过`.show()`方法将其显示,关闭时使用`.hide()`方法隐藏。 - 基本代码示例...
总的来说,"js alert confirm样式弹出框.zip"项目展示了如何通过前端技术自定义JavaScript内置对话框的样式,以提供更加美观和一致的用户体验。这涉及到HTML5结构化内容、CSS样式设计以及jQuery的事件处理和DOM操作...
然而,为了模拟Android的提示框效果,我们需要编写自定义的JavaScript代码来创建一个可定制的元素,比如一个div,然后通过CSS控制其样式和动画效果。我们可以创建一个包含提示信息的HTML元素,例如: ```html ...
`alert()`函数是JavaScript内置的一种用户交互方式,用于弹出一个带有消息的对话框,通常包含一个“确定”按钮。然而,`alert()`对话框的样式和功能较为单一,不能满足所有设计需求。在某些情况下,开发者可能希望...
5. **自定义样式**:为了让弹出框与网站设计相协调,通常需要添加CSS来调整布局、颜色、字体等样式。可以使用内联样式、类选择器或ID选择器进行设置。 6. **响应式设计**:考虑到现代网页需要适应各种屏幕尺寸,弹...
"alert和confirm弹出框样式美化2"的主题就是如何通过CSS和JavaScript来定制这两个对话框的外观,提升用户体验。 `alert` 对话框主要用于通知用户,展示一条重要的信息,并且只有一个“确定”按钮。`confirm` 对话框...
在JavaScript编程中,"showConfirmBox"通常是指一个自定义的弹出框插件,用于模拟浏览器原生的`alert`、`confirm`和`prompt`功能,提供更丰富的交互和自定义样式。这个插件可能包含多个功能,如显示警告信息、请求...
这篇博客“JS模拟窗口、弹出层提示框等……”可能探讨了如何使用JavaScript来创建自定义的对话框,而不是依赖浏览器内置的alert(), prompt()和confirm()函数。 1. **模拟窗口(Custom Dialogs)**: - 模拟窗口是...
在JavaScript编程中,有时我们可能需要自定义弹出层来替代浏览器原生的`alert`、`prompt`或`confirm`对话框,以提供更丰富的交互体验或者在特定场景下,比如在`iframe`中使用时,保持页面的可操作性。本主题将详细...
在网页交互中,弹出框是常见的功能,它能够提供与用户交互的界面,比如提示信息、确认操作或输入数据。本篇文章将深入探讨如何在所有浏览器中实现JS弹框,并介绍相关知识点。 1. `alert()`函数:这是最基础的JS弹框...
在`msg.js`中,开发者可能通过事件监听和DOM操作来模拟`alert`和`confirm`的行为,当用户点击自定义对话框的按钮时,触发相应的回调函数,并返回预期的布尔值。 实现这样的功能通常涉及以下步骤: 1. 创建HTML结构...
可以直接调用,操作方便,调用代码如下: jConfirm('Can you confirm this,<span am red.</span>?', 'Confirmation Dialog', function(r) { });