CSS让div悬浮

html-css010

CSS让div悬浮,第1张

.tl {

width: 240px

height: 193px

position: absolute/*这里一定要设置*/

z-index: 999999/*这里是该元素与显示屏的距离,据说越大越好,因为没有它也是可以的*/

margin-top: 20%

margin-left: -209px

background-image :url("/ship_three/images/tl.png")

-webkit-transition: .5s ease-in-out/* css的transition允许css的属性值在一定的时间内从一个状态平滑的过渡到另一个状态 */

-moz-transition: .5s ease-in-out/*这里为了兼容其他浏览器*/

-o-transition: .5s ease-in-out

background-image: url("/ship_three/images/tl.png")

}

可以,没问题CSS代码就是上面的。

扩展资料:

div中style使用css代码

div中可以直接写CSS样式代码,只需要DIV代码(标签)中使用style属性即可直接写CSS样式。

DIV代码:

<div style="color:#F00border:1px solid #000width:300pxheight:100px">你好 DIVCSS5</div> 

完整案例代码与效果截图:

完整HTML案例代码

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html charset=utf-8" />

<title>DIV直接写CSS DIVCSS5案例</title>

</head>

<body>

<div style="color:#F00border:1px solid #000width:300pxheight:100px">你好 DIVCSS5</div>

</body>

</html>

div标签内常用属性列表

1、style 设置css样式(扩展了解style标签)

2、align 设置div盒子内的内容居中、居左、居右

3、id 引人外部对应#(井号)选择符号样式

4、class 引人外部对应.(句号)选择符号样式

5、title 设置div(标题)鼠标经过时显示文字(扩展了解 title标签)

参考资料来源:DIV-百度百科

左浮动即float:left,使布局元素靠左浮动。右浮动即float:right,使布局元素靠右浮动。在网页布局中结合这两种布局方式可以设计出千变万化的布局形式!只要发散你的思维,开拓你的想象相信定能设计出好的作品!祝你成功!

完美的封装了popUpBox这个类

这个类一共两个公开方法

show    显示

hidden    隐藏

其他功能请自行扩展

不敢保证简单,但每一行都写了注释

<!DOCTYPE html>

<html>

<head>

<title>CSS 点击文字或按钮打开div浮动层 </title>

<meta charset="UTF-8" />

</head>

<script>

function popUpBox(){ //创建盒子类

this.init = function(){ //初始化函数

var onloadFunc = window.onload, //将window.onload事件执行的函数保存到onloadFunc

subInit = function(o){ //需要等结构加载完成执行的函数

document.body.appendChild(o.mask) //将遮罩添加到body

document.body.appendChild(o.box) //将盒子添加到body

},

createElement = function(){ //创建元素函数,共同的样式在这里已经设置

var e = document.createElement('div') //创建一个div

e.style.position = 'fixed' //设置定位方式为跟随浏览器移动

e.style.left = '0px' //元素x轴位置

e.style.top = '0px' //元素y轴位置

e.style.zIndex = '2000' //元素z轴位置

e.style.width = window.innerWidth + 'px' //元素宽度

e.style.height = window.innerHeight + 'px' //元素高度

e.style.margin = '0px' //元素的外边距

e.style.padding = '0px' //元素的内边距

e.style.backgroundColor = 'rgba(0, 0, 0, 0.3)' //元素的背景颜色

return e //返回这个元素

}

this.mask = createElement() //创建遮罩

this.mask.onclick = (function(o){ //创建匿名函数 参数一 传入需要操作的对象

return function(){ //匿名函数返回为遮罩绑定鼠标点击事件执行函数

o.hidden() //为o这个对象执行隐藏函数

}

})(this) //立即执行匿名函数 并传入当前对象

this.box = createElement() //盒子

this.box.style.left = window.innerWidth / 2 + 'px' //设置盒子水平居中

this.box.style.top = window.innerHeight / 2 + 'px' //设置盒子垂直居中

this.box.style.width = '400px' //设置盒子宽度

this.box.style.height = '300px' //设置盒子高度

this.box.style.border = '1px solid #3399ff' //设置边框

this.box.style.borderRadius = '5px' //设置圆的边框

this.box.style.zIndex = '2001' //设置盒子的z轴

this.box.style.backgroundColor = '#FFF' //设置盒子的背景颜色

this.box.style.transform = 'translate(-50%, -50%)' //执行2d动画,把这个盒子位置向反方向移动50%

this.box.innerHTML = '在这里设置盒子的内容,关闭盒子调用实例的hidden()函数即可,如这个实例想这样 loginBox.hidden()//就可以隐藏 '

this.hidden() //隐藏遮罩和盒子

window.onload = (function(o){ //创建匿名函数 参数一 要操作的对象

return function(){ //返回onload事件执行的函数

if(typeof onloadFunc == 'function'){ //判断onloadFunc中有没有函数,防止覆盖其他功能的执行事件

onloadFunc() //执行已经绑定的函数

}

subInit(o) //执行初始化需要执行的函数

}

})(this) //立即执行匿名函数 并传入当前对象

}

this.show = function(){ //显示

this.mask.style.display = 'block'

this.box.style.display = 'block'

}

this.hidden = function(){ //隐藏

this.mask.style.display = 'none'

this.box.style.display = 'none'

}

this.init() //调用初始化函数

}

var loginBox = new popUpBox() //创建一个实例,可以创建N个,互相不干扰

</script>

<body>

<a href="#" onclick="loginBox.show()">弹出盒子</a>

</body>

</html>