4

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>
4

5 に答える 5

3

同じテキストで 2 つの div を重ねて配置し、そのうちの 1 つをクリックしてアニメーション化します。

HTML

<div class="red">Text</div>
<div class="black">Text</div>

CSS

div {
    position: absolute;
    top: 0px;
    left: 0px;
    font-size: 48px;
    font-family: arial;
    font-weight: bold;
    width: 150px;
    overflow: hidden;
    cursor: pointer;
}
div.red { color: red;}

Javascript

$(document).ready(function() {
    $("div.black").animate({
        'width': 0
    }, 2000);
});

http://jsfiddle.net/d8LAN/3/

于 2013-07-19T12:32:39.050 に答える
0

プレーンな JavaScript で次のように実行できます。

function animate() {
    var logo = document.getElementById('logo');
    logo.style.color = 'blue';
    logo.style.backgroundColor = '#ff8';
    // http://stackoverflow.com/questions/5598743/finding-elements-position-relative-to-the-document
    var elem = logo;
    var offset = { top: 0, left: 0 };
    do {
        if (!isNaN(elem.offsetLeft)) {
            offset.top += elem.offsetTop;
            offset.left += elem.offsetLeft;
        }
    } while (null !== (elem = elem.offsetParent));
    var c = logo.cloneNode(true);
    c.id = '';
    c.style.position = 'absolute';
    c.style.top = offset.top + 'px';
    c.style.left = offset.left + 'px';
    c.style.color = 'red';
    c.style.backgroundColor = '#8ff';
    c.style.width = 0;
    c.style.height = logo.clientHeight + 'px';
    document.body.appendChild(c);
    //logo.parentNode.insertBefore(c, logo.nextSibling); //(alternative)
    var maxw = logo.clientWidth,
        currw = 0;
    var duration = 1000; //miliseconds
    var iv = setInterval(function () {
        currw += 1;
        if (currw > maxw) {
            clearInterval(iv);
        }
        c.style.width = currw + 'px';
    }, (duration/maxw));
}

http://jsfiddle.net/SkfHx/ (完全なコード)
http://jsfiddle.net/SkfHx/show (結果、IE7+ および他のすべてのブラウザーで適切に動作します)

于 2013-07-19T13:44:32.447 に答える
0

CSS のみ : HTML 内 :-

<h1>Online<a><span>Online</span></a></h1>

そしてCSSで: -

h1 {
    position: absolute;
    top: 0;
    left: 0;
    height: 100px;
    line-height:100px;
    width: 280px;
    text-align: center;
    font-size:70px;
    color:#fff;
   background:rgba(171,169,170,0.8);
    padding:0px; 
    margin:0px;
    cursor:pointer;

}
     h1 a {
        position: absolute;
        top: 0;
        left: 0;
        height: 0px;
        width: 0px;
        text-align: center;
        font-size: 70px;
        color:#fff;
        overflow:hidden;
        padding: 0px;
        margin: 0px;
 -webkit-transition:     all 400ms cubic-bezier(0.785, 0.135, 0.15, 0.86);
    }
   h1:hover a{
    height: 100px;
    width: 280px;
 -webkit-transition:     all 400ms cubic-bezier(0.785, 0.135, 0.15, 0.86);

    }
     h1 a span { position: absolute;
        top: 0;
        left: 0;
         font-size: 70px; height: 100px;
    width: 280px;
    color:rgba(0, 148, 255, 0.84);
    cursor:pointer;
    }
于 2013-10-31T20:51:29.990 に答える