带有JavaScript和CSS的动画图像(如非GIF)
Animated image (e.g. non-GIF) with JavaScript and CSS?
在使用此方法时,我一直在尝试用JavaScript制作.png文件的动画。这是我想要制作动画的.png文件。我希望动画标志是正确的"Cupcquake",但标志甚至没有出现,更不用说动画了。但是,将HTML中的"span class=logoCquake"更改为div类会显示徽标,但它位于文本下方
我的JavaScript文件:
var scrollUp = (function () {
var timerId;
return function (height, times, element) {
var i = 0;
timerId = setInterval(function () {
if (i > times)
i = 0;
element.style.backgroundPosition = "0px -" + i * height + 'px';
i++;
}, 100);
};
})();
scrollUp(130, 30, document.getElementByClass('logoCquake'))
我的HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="../css/normalize.css" />
<link rel="stylesheet" href="../css/style.css" />
<script src="../scripts/logoCquake.js" type="text/javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cupquake - Home</title>
</head>
<body>
<div class="wrapper">
<div class="header">
<div class="topbar">
<div class="mCquake">
</div>
</div>
<br>
<br>
<br>
<span class="ninja">Ninja</span><span class="cupquake">Cupquake</span><span class="logoCquake"></span>
</div>
</div>
</body>
</html>
CSS文件:
@font-face
{
font-family: typo_semib;
src: url('fonts/typo_semib.ttf');
}
@font-face
{
font-family: typo_light;
src: url('fonts/typo_light.ttf');
}
.wrapper .header
{
height: 250px;
width: 100%;
background: -webkit-gradient(linear,left top,left bottom,color-stop(0%,#ffa200), color-stop(100%,#d25400));
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffa200',endColorstr='#d25400',GradientType=0);
background: -moz-linear-gradient(top,#ffa200,#d25400);
background-image: -o-linear-gradient(#ffa200,#d25400);
overflow: hidden;
}
.wrapper .header .topbar
{
height: 60px;
background-image: url(../imgz/head/hBarSBg.png);
background-repeat: repeat-x;
}
.wrapper .header .topbar .mCquake
{
height: 37px;
width: 278px;
background-image: url(../imgz/head/mCqRight.png);
background-repeat: no-repeat;
float: none;
float: right !important;
margin-right: 10px;
margin-top: 11.5px;
margin-bottom: 11.5px;
}
.wrapper .header .ninja
{
font-family: typo_semib;
font-size: 48px;
color: #303030;
margin-left: 55px;
}
.wrapper .header .cupquake
{
font-family: typo_light;
font-size: 48px;
color: #303030;
}
.wrapper .header .logoCquake
{
height: 112px;
width: 130px;
background-image: url(../imgz/logo/logoCquake.png);
background-repeat: no-repeat;
}
编辑:
再次尝试,但使用此处列出的第二种方法,仍然一无所获。以下是我当前的HTML和JS代码:
JS:
function SpriteAnim (options) {
var timerId,
i = 0,
element = document.getElementByClass(options.elementClass);
element.style.width = options.width + "px";
element.style.height = options.height + "px";
element.style.backgroundRepeat = "no-repeat";
element.style.backgroundImage = "url(" + options.sprite + ")";
timerId = setInterval(function () {
if (i >= options.frames) {
i = 0;
}
element.style.backgroundPosition = "0px -" + i * options.height + "px";
i ++;
}, 100);
this.stopAnimation = function () {
clearInterval(timerId);
};
}
var cupcake = new SpriteAnim({
width: 130,
height: 112,
frames: 30,
sprite: "..'imgz'logo'logoCquake.png",
elementClass : "logoCquake"
});
HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="../css/normalize.css" />
<link rel="stylesheet" href="../css/style.css" />
<script language="javascript" src="SpriteAnim.js">
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cupquake - Home</title>
</head>
<body>
<div class="header">
<div class="topbar">
<div class="mCquake">
</div>
</div>
<span class="ninja">Ninja </span><span class="cupquake">Cupquake</span><span class="logoCquake"></span>
</div>
</div>
</body>
</html>
我的代码似乎可以工作:
<!doctype html>
<style>
div{background:url(http://i1243.photobucket.com/albums/gg555/Nyanja/logoCquake.png);height:33px;width:39px}
</style>
<div id=anim></div>
<script>
var i=0;
setInterval(function(){i+=33.6;document.getElementById("anim").style.backgroundPosition="0px "+i+"px"},100)
</script>
我相信"span"标记是一个内联元素,它要么需要"display:block"属性,要么需要"float:left",我尝试了你的代码,在我用类"logoCquake"将"display:block"添加到"span"标签后,它对我有效。希望这对你有帮助。
注意:如果使用"display:block",则span将移动到新行,如果使用"float:left",则"span"标记将移动到文本的左侧。
相关文章:
- 如何在Mocha/Chai中测试JS原型(非模块)
- 如何在JavaScript中剥离数组元素中的非整数
- 如何在three.js中加载指定文件内容而非路径的三维模型
- 如何在onclick事件执行代码时在ImageButton上设置加载gif
- 如何在Javascript中编写非阻塞回调
- 忽略任何非小写字符(如数字、大写字母和符号)并获取k的值
- 如何在 JavaScript 中获取非重复字符及其计数
- 如何在 Google 应用脚本中 sendEmail 函数的范围内的唯一非空白列中返回值
- 如何在angular中访问URL中的查询参数(在hash之外,非html 5模式)
- 如何在openlayers-3中正确旋转保持圆形(而非椭圆形)的矢量
- 如何在Github电子原子中禁用或使浏览器窗口处于非活动状态
- 如何在 JavaScript 中实现用于设置和获取选项的非 CQS API
- 如何在 HTML5 视频元素中播放电影时在 IOS 上显示非全尺寸视频
- 如何在javascript中使用非拉丁字符作为字符串
- 如何在 Javascript 中解析非英语文本日期
- 如何在 angularjs 指令中调用非 jquery 对象方法
- 如何在 SVG 上的鼠标光标旁边显示工具提示,尽管网站上有非 SVG 元素
- 如何在 Typescript 1.8 中包含非类型化节点模块
- 带有JavaScript和CSS的动画图像(如非GIF)
- 如何在gif动画完成后激活页面滚动