gpt4 book ai didi

javascript - 使用 CSS 或 Javascript 填充动画

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:16:15 24 4
gpt4 key购买 nike

我只是想知道是否可以使用 CSS 或 javascript 创建填充动画?

基本上我想创建一个如下图所示的填充动画:

http://i40.tinypic.com/eit6ia.png

红色为填充,黑色为空。

这是 Jeff 为图像发布的我的代码,但由于某种原因它不起作用!我错过了什么吗??

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
#black{
position:absolute;
z-index:2;
color:black;
font-weight:bold;
font-size:2em;
width:768px;
}
#red {
position:absolute;
z-index:10;
left:8px;
width:0px;
overflow:hidden;
font-weight:bold;
font-size:2em;
color:red;
}
</style>

<script language="javascript">
var red = document.getElementById('red');

red.style.width = "0px";
red.style.height = "1024px";
var animation = setInterval(function(){

if(parseInt(red.style.width,10) == 768)
clearInterval(animation);
red.style.width = parseInt(red.style.width,10)+2 +"px";
},10);
</script>


</head>

<body>
<div><img id="black" src="http://www.ratemotorcycle.com/image1/5/53/kawasaki-ninja-zx10r-2013.jpg"/><img id="red" src="http://imageshack.us/a/img39/3517/13kawasakizx10r1.jpg"/></div>
</body>
</html>

最佳答案

这是纯 JavaScript 中的一种方式。我对 CSS 样式很感兴趣。

Live Demo(with text)

Live Demo (with images)

图片动画

HTML

<div>
<img id="black" src="http://www.ratemotorcycle.com/image1/5/53/kawasaki-ninja-zx10r-2013.jpg" />
<img id="red" src="http://imageshack.us/a/img39/3517/13kawasakizx10r1.jpg" />
</div>

CSS

#black {
/* Added the position and the z-index to ensure images overlay correctly */
position:absolute;
z-index:2;

/* styling fun */
color:black;
font-weight:bold;
font-size:2em;

/* default width */
width:768px;
}
#red {
/* Position and z-index let us overlay the #black and #red elements*/
position:absolute;
z-index:10;

/* This is to put the element directly over the #black element
(compensating the margin/padding of the #black element */
left:8px;

/* We make the initial width 0px and hide the overflow. */
width:0px;
overflow:hidden;

/* Fun styling */
font-weight:bold;
font-size:2em;
color:red;
}

JavaScript

var red = document.getElementById('red');

red.style.width = "0px";
/* set the height so the image does not "scale" */
red.style.height = "1024px";
var animation = setInterval(function () {

if (parseInt(red.style.width, 10) == 768) clearInterval(animation);
red.style.width = parseInt(red.style.width, 10) + 2 + "px";
}, 10);



文字动画

HTML

<div> 
<span id="black">LOGO</span>
<span id="red">LOGO</span>
</div>

CSS

#black{
/* Fun styling */
color:black;
font-weight:bold;
font-size:2em;
}
#red {
/* Position and z-index lets us overlay the #black and #red elements*/
position:absolute;
z-index:10;

/* This is to put the element directly over the #black element
(compensating the margin/padding of the #black element */
left:8px;

/* We make the initial width 0px and hide the overflow. */
width:0px;
overflow:hidden;

/* Fun styling */
font-weight:bold;
font-size:2em;
color:red;
}

JavaScript

/* On window load (when the page is loaded) */
window.onload = function(){
/* We select our #red element */
var red = document.getElementById('red');

/* Set the width of #red to 0px */
red.style.width = "0px";

/* Create an action that will execute every 50ms */
var animation = setInterval(function(){

/* If the element is at the desired width, we clear the animation loop */
if(red.style.width == "91px")
clearInterval(animation);

/* Otherwise, we set the width+=1, ensuring to get the numeric value of it only */
red.style.width = parseInt(red.style.width,10)+1 +"px";
},50);
};

jQuery 替代品

$('#red').css('width','0px');
$('#red').animate({'width':'91px'},4500);

JavaScript 与 jQuery

JSPerf results

经过非官方电池测试后,用于此目的的 jQuery 动画的运行速度将比其纯 JavaScript 动画慢 85%。这是一个巨大的性能差异。

关于javascript - 使用 CSS 或 Javascript 填充动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17745983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com