css图片慢慢出现效果

html-css027

css图片慢慢出现效果,第1张

利用css3来实现图片渐入效果。

下面是页面中的一段html:

<div class="features">

<div class="container">

<div class="inner feature">

<div class="content">

<h2 class="title"><img src="./imgs/title1.png" alt=""/></h2>

<p class="text">256位SSL加密安全连接,手机短信验证、谷歌两步验证、资金密码、邮箱验证四重验证保障安全,钱包分布式离线冷存储</p>

</div>

<img class="icon" src="./imgs/feature-icon1.png" alt=""/>

</div>

</div>

<div class="container middle">

<div class="inner feature">

<div class="content">

<h2 class="title"><img src="./imgs/title1.png" alt=""/></h2>

<p class="text">256位SSL加密安全连接,手机短信验证、谷歌两步验证、资金密码、邮箱验证四重验证保障安全,钱包分布式离线冷存储</p>

</div>

<img class="icon" src="./imgs/feature-icon2.png" alt=""/>

</div>

</div>

<div class="container">

<div class="inner feature">

<div class="content">

<h2 class="title"><img src="./imgs/title1.png" alt=""/></h2>

<p class="text">256位SSL加密安全连接,手机短信验证、谷歌两步验证、资金密码、邮箱验证四重验证保障安全,钱包分布式离线冷存储</p>

</div>

<img class="icon" src="./imgs/feature-icon3.png" alt=""/>

</div>

</div>

</div>

首先,需要先定位图片,把图片定位在渐入后最终停留的位置:

.feature .icon { position: absolute top: 100px

}

接着,利用css3的transform属性和translate()方法实现图片的偏移,还有就是将图片透明度设为0(即完全透明):

.feature .icon {position: absolute top: 100px transform: translate3d(0, 0, 150px) -ms-transform: translate3d(0, 150px, 0) -webkit-transform: translate3d(0, 150px, 0) -o-transform: translate3d(0, 150px, 0) -moz-transform: translate3d(0, 150px, 0) opacity: 0

}

这边需要加上各浏览器的前缀来兼容一些低版本浏览器。这些属性和方法的具体用法这边就细讲了。

然后,要用到的是css3的transition属性:

.feature .icon {position: absolute top: 100px transform: translate3d(0, 0, 150px) -ms-transform: translate3d(0, 150px, 0) -webkit-transform: translate3d(0, 150px, 0) -o-transform: translate3d(0, 150px, 0) -moz-transform: translate3d(0, 150px, 0) opacity: 0 transition: transform 1s ease 0s, opacity 1s ease 0s -moz-transition: -moz-transform 1s ease 0s, opacity 1s ease 0s -webkit-transition: -webkit-transform 1s ease 0s, opacity 1s ease 0s -o-transition: -o-transform 1s ease 0s, opacity 1s ease 0s -ms-transition: -ms-transform 1s ease 0s, opacity 1s ease 0s

}

transition属性是一个过渡属性,当元素从一种样式变换为另一种样式时为元素添加效果。

到这还没有效果。我们要达到的效果是:当网页往下滚动,图片出现或将要出现在视窗时,我们来触发这个过渡效果,就像大家看到的下面这张图片一样。这个做法就像图片的惰性加载,图片还没出现在视窗中时,先不加载,出现时再去加载图片,这样的效果就是用户访问页面的速度提升了。

把$("#testTable").mouseout改成$("#testTable").mouseleave

mouseout在鼠标移入子元素时也会触发,而mouseleave只会在鼠标移出本元素时触发

以下是完整代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

<HEAD>

<TITLE>New Document </TITLE>

<META NAME="Generator" CONTENT="EditPlus">

<META NAME="Author" CONTENT="">

<META NAME="Keywords" CONTENT="">

<META NAME="Description" CONTENT="">

<SCRIPT LANGUAGE="JavaScript" src="jquery.js"></SCRIPT>

<SCRIPT LANGUAGE="JavaScript">

//-----------------------------

var slowObj

var objWidth

var objHeight

var iter

var operator

var flag = true

function SlowDiv2(tableId,operator2){

slowObj = $("#"+tableId)

if(flag){

objWidth = slowObj.width()

objHeight = slowObj.height()

flag = false

}

slowObj.css({"overflow":"hidden"})

operator = operator2

if(operator=="show"){

slowObj.width(0)

slowObj.height(0)

}

iter = setInterval(operateTable,14)

}

function operateTable(){

var tmpWidth = slowObj.width()

var tmpHeight = slowObj.height()

if(operator=="hide"){

//隐藏table

if( (tmpWidth<=18 || tmpWidth-10<=0 ) &(tmpHeight<=18 || tmpHeight<=0)){

var d = clearInterval(iter)

slowObj.hide()

return

}else{

slowObj.width(tmpWidth-10)

slowObj.height(tmpHeight-10)

$("#ttest").val("-"+slowObj.width())

}

//$("#ttest").val(objWidth)

}else{

//显示table

slowObj.show()

if(tmpWidth+10>objWidth || tmpHeight+10>objHeight){

var d = clearInterval(iter)

slowObj.width(objWidth)

slowObj.height(objHeight)

return

}else{

slowObj.width(tmpWidth+20)

slowObj.height(tmpHeight+2)

$("#ttest").val("+"+slowObj.width())

}

// $("#ttest").val(objWidth)

}

}

$(document).ready(function(){

$("#S_switch").mouseover(function(){

SlowDiv2("testTable","show")

$("#S_switch").focus(function(){

$("#ttest").val("#S_switch")

})

}).mouseout(function(){

//alert("#S_switch out")

//SlowDiv2("testTable","show")

})

$("#testTable").mouseleave(function(){

// $("#ttest").val("table out")

SlowDiv2("testTable","hide")

}).mouseover(function(){

})

$("#testTable tr").mouseleave(function(){

//alert("tr out")

}).mouseover(function(){

//alert("tr over")

})

$("#testTable tr").css("background-color","pink")

})

</SCRIPT>

</HEAD>

<BODY>

<p id="S_switch" style="width:20pxbackground-color:grayfloat:lefttext-align:center">

<b>>></b>

</p>

<!-- div --->

<div style="width:302height:200pxbackground-color:pinkdisplay:none" id="testTable" >

<div style="background-color:#123abcwidth:100%">123213</div>

saf师大<br/>

1<br/>

1<br/>

</div>

</BODY>

</HTML>