2016-06-01 18 views
2

CSSキーフレームを使用して固定した配置で拡大する円があります。問題は、円がサイズを変えるときに、円の中心が動く(左上が固定されている間)。アニメーション中にセンターが固定されていることを確認するにはどうすればよいですか? divの "起源"を指定する方法はありますか?それは左上ではありませんか?サイズ変更時にオブジェクト中心を固定する

<div id="circle"></div> 

#circle { 
    position: fixed; 
    background: #07F; 
    border-radius: 50%; 
    animation: expand linear 3s infinite; 
    top: 10px; 
    left: 10px; 
} 

@keyframes expand { 
    0% { 
    width: 100px; 
    height: 100px; 
    } 
    100% { 
    width: 500px; 
    height: 500px; 
    } 
} 

this JSFiddle

答えて

4

別のオプトインをイオン、何を行うことができますが、それが中心に保つために寸法を高めるためにtransform - scaleプロパティを使用して、transform-originです:

#circle { 
 
    position: fixed; 
 
    background: #07F; 
 
    border-radius: 50%; 
 
    animation: expand linear 3s infinite alternate; 
 
    top: 10px; 
 
    left: 10px; 
 
    transform-origin: center; 
 
    width: 100px; 
 
    height: 100px; 
 
} 
 
@keyframes expand { 
 
    0% { 
 
    transform: scale(1); 
 
    } 
 
    100% { 
 
    transform: scale(5); 
 
    } 
 
}
<div id="circle"></div>

+0

まさに私が探していたものです! – binaryfunt

+0

嬉しいことにU @ BinaryFunt – DaniP

3

はこれを試してください:

#circle { 
 
    position: fixed; 
 
    background: #07F; 
 
    border-radius: 50%; 
 
    animation: expand linear 3s infinite alternate; 
 
    top: calc(10px + 50px); /* 10px + (half of the initial height) */ 
 
    left: calc(10px + 50px); /* 10px + (half of the initial width) */ 
 
    transform: translate(-50%, -50%) 
 
} 
 

 
@keyframes expand { 
 
    0% { 
 
    width: 100px; 
 
    height: 100px; 
 
    } 
 
    100% { 
 
    width: 500px; 
 
    height: 500px; 
 
    } 
 
}
<div id="circle"></div>

+0

私は否定的に考えていた変換が、 'calc'の一部を欠落していたニース1 +1 – DaniP

+0

実際には、後でサークルに罫線を追加し、尺度変換によって枠線がピクセル化され、その太さが変更されるので、このメソッドに切り替えることになりました。 – binaryfunt

関連する問題