JavaScript怎么设置d出的对话框的大小?

JavaScript怎么设置d出的对话框的大小?,第1张

alert是不行的 你用window.open(PageUrl,name,parameters) 打开窗口试试 这个就可以设置大小 如window.open('page.html','newWindwo','height=300,width=300')

ajaxmenu根本就没有实现d出DIV大小的参数!所以上面那段代码不能改变d出窗口大小!要控制大小自已写一个函数下面是个例子://创建窗口

/*

* 创建窗口(参数说明)

* wWidth:int 窗口宽度

* wHeight:int 窗口高度

* wMainID:主窗口id

* wTitleID:string 标题ID

* wTitleText:string 标题内容

* wShowID:string 窗口要显示的位置(层ID)

* wFileURL:string 模板文件位置

*/

function CreateWindow(wWidth, wHeight, wMainID, wTitleID, wTitleText, wShowID, wFileURL) {

xmlHttp = initXMLHttpClient()

xmlHttp.open("GET", wFileURL, false)

xmlHttp.onreadystatechange = function() {

if (xmlHttp.readyState == 4) {

var response = xmlHttp.responseText

var tHeight = window.screen.availHeight //alert("创建窗口函数:" + window.screen.availHeight)

document.getElementById(wShowID).innerHTML = response

document.getElementById(wMainID).style.width = wWidth

document.getElementById(wMainID).style.height = wHeight

document.getElementById(wMainID).style.position = "absolute"

document.getElementById(wMainID).style.left = document.body.clientWidth / 2 - wWidth / 2

//document.GetElementById(wMainID).style.top = "50%"

document.getElementById(wTitleID).innerText = wTitleText

wTether = true

wWindow = 0

wCount++

}

}

xmlHttp.send(null)

}

首先回忆一下d窗的实现,一般我们分为两层,d出窗口层(popus)和遮罩层(mask),通常情况下我习惯就这两元素全部设成fixed定位,具体和absolute区别一试便知。对于mask层自不用多少,我们如下给他设置属性,让他铺满整个屏幕。

.mask{position:fixedtop:0pxbottom:0pxleft:0pxright:0pxbackground-color:#000opacity:0.6filter:alpha(opacity=60)}

popus层则要稍微麻烦点儿,这里我们有两种实现方法

1.已知大小的d窗,如下,主要通过top,left与负的margin来实现。

.popus{width:300pxheight:200pxposition:fixedleft:50%top:50%margin-left:-150pxmargin-top:-100pxbackground-color:#000}

2.未知d窗大小,则通过js获取d窗层的width与height,然后在进行如上设置,在此不多述。

3.在支持css3的情况下,我们不需要知道d窗的宽高,便可进行如下设置

.popus{position:fixedleft:50%top:50%transform:translate(-50%,-50%)}

主要通过translate属性来设置,偏移的值百分比是相对于本身的宽高,因此从原理上来说跟第一种写法有异曲同工之妙,不过使用却更方便。

言归正传,下面我们回归到正题,即让元素实现ps中高斯模糊的效果。

这里引出一个css属性:filter,注意这里的filter并不是ie中的filter,filter有很多值,感兴趣的可以点击这里,作者讲的非常详细。我们今天只讲其中的一个blur,首先看下面的预览图

ps:目前来说该属性只支持webkit浏览器,所以我们直接使用了css3属性,效果也需要在webkit浏览器中查看

是不是很神奇,其中起作用的代码就这一行 -webkit-filter:blur(8px) ,后面的像素值即代表模糊程度,当然在日常项目中,我们还可以加一些动画,使页面更加的生动,本案例完整代码如下:

<div class='bg'>

<img src='bg.jpg' />

</div>

<div class='popus'>

效果是不是要好过纯色加透明呢

<div>

<div class='left btn '>确实不错</div>

<div class='right btn'>也就那样</div>

</div>

</div>

css:

*{padding:0pxmargin:0px}

img{width:100%margin:0px autodisplay:block}

.bg.blur{-webkit-filter:blur(8px)}

.popus{width:400pxcolor:#000position:fixedtop:50%left:50%-webkit-transform:translate(-50%,-50%)font-family:"微软雅黑"padding:20px 0pxfont-weight:boldbackground-color:rgba(255,255,255,0.6)border-radius:18pxtext-align:centerpadding:30px 0pxbox-shadow:0px 0px 10px rgba(0,0,0,0.4)display:none}

.popus div{width:220pxmargin:10px auto}

.popus div.btn{width:80pxpadding:5px 10pxcolor:#000}

.left{float:leftborder:1px solid #000}

.popus div.btn.right{float:rightcolor:#666}

js:

$('.bg').on('click',function(){

console.log(98)

$(this).addClass('blur')

$('.popus').show()

})

$('.btn').on('click',function(){

$('.bg').removeClass('blur')

$('.popus').hide()

})

这样是不是就完了?很明显不是,看控制台

当我们d出窗口外,肯定要禁止掉我们其他层的点击事件,但是我们发现目前我们虽然将其他层模糊化了,但是并没有禁止掉相应的事件,当然解决办法也很简单,我们可以加一层没有背景颜色的遮罩层,覆盖在页面上,这样我们每次点击作用在遮罩层上,自然不会触发底层的事件了。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11311335.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存