導航:首頁 > 編程語言 > 用js實現css動畫效果

用js實現css動畫效果

發布時間:2021-02-27 21:53:41

『壹』 css3 實現動畫效果,怎樣使他無限循環動下去

主要需要使用 -webkit-animation
如:
-webkit-animation:gogogo 2s infinite linear ;
其中gogogo是自己定義的動畫幀,2s是整個動畫的秒數,infinite是永久循環 linear是線性變化 (step-end則是無線性變化,直接輸出結果)

代碼如下:
CSS:

@-webkit-keyframes gogogo {
0%{

-webkit-transform: rotate(0deg);
border:5px solid red;

}
50%{
-webkit-transform: rotate(180deg);
background:black;
border:5px solid yellow;
}
100%{
-webkit-transform: rotate(360deg);
background:white;
border:5px solid red;
}

}

.loading{
border:5px solid black;
border-radius:40px;
width: 28px;
height: 188px;
-webkit-animation:gogogo 2s infinite linear ;
margin:100px;

}

『貳』 請問用js或者css3怎麼能實現元素邊框動畫效果,如圖!

無需js css3直接能搞定材料:3張png (背景框框,兩個發光的點點);布局用定位做css3裡面的回animation做動畫hover觸發動畫請問答用js或者css3怎麼能實現元素邊框動畫效果,如圖!

『叄』 如何使用javaScript控制CSS Animations和Transitions

有時候WEB開發人員認為的動畫比JavaScript的動畫更難理解。雖然CSS動畫有其局限性,但它的性能比大多數JavaScript庫更加高效,因為它可以藉助硬體加速啊!其效果絕對可以超出我們的預期。

CSS animations和transitions再加上點JavaScript就可以實現硬體加速動畫,而且其交互效果比大多數JavaScript庫更高效。

So,讓我們快點開始吧!小夥伴們都等不及了!

注意:Animations(動畫)和Transitions(過渡)是不同的

CSS Transitions(過渡)被應用於元素指定的屬性變化時,該屬性經過一段時間逐漸的過渡到最終需要的值;而CSS Animations(動畫)只是在應用時執行之前定義好的操作,它提供更細粒度的控制。

在這篇文章中,我們將分別針對上述內容進行講解。

控制CSS Transition(過渡)

在編程論壇中,關於transition(過渡)的觸發和暫停有無數的疑問。使用JavaScript可以很容易的解決這些疑問。

如何觸發元素的transiton(過渡)?切換元素的類名可以觸發該元素的transition(過渡)

如何暫停元素的transition(過渡)? 在你想要暫停過渡點,用getComputedStyle和getPropertyValue獲取該元素相應的CSS屬性值,然後設置該元素的對應的CSS屬性等於你剛才獲取到的CSS屬性值。

以下是該方法的一個例子。

<!DOCTYPE html>
<html>
<head>
<title>操作transtition</title>
<style type="text/css">
.box {
margin: 30px;
height: 50px;
width: 50px;
background-color: blue;
}
.box.horizTranslate {
-webkit-transition: 3s;
-moz-transition: 3s;
-ms-transition: 3s;
-o-transition: 3s;
transition: 3s;
margin-left: 50% !important;
}
</style>
<script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>
<h3>Pure Javascript</h3>
<div class='box'></div>
<button class='toggleButton' value='play'>Play</button>
<h3>jQuery</h3>
<div class='box'></div>
<button class='toggleButton' value='play'>Play</button>
<script type="text/javascript">
var boxOne = document.getElementsByClassName('box')[0],
boxTwo = $(".box:eq(1)");
document.getElementsByClassName('toggleButton')[0].onclick = function(){
if(this.innerHTML === 'Play'){
this.innerHTML = 'Pause';
boxOne.classList.add('horizTranslate');
}else{
this.innerHTML = 'Play';
var computedStyle = window.getComputedStyle(boxOne),
marginLeft = computedStyle.getPropertyValue("margin-left");
boxOne.style.marginLeft = marginLeft;
boxOne.classList.remove('horizTranslate');
}
}
$('.toggleButton:eq(1)').on('click',function(){
if($(this).html() === 'Play'){
$(this).html('Pause');
boxTwo.addClass('horizTranslate');
}else{
$(this).html('Play');
var computedStyle = boxTwo.css('margin-left');
boxTwo.css('margin-left',computedStyle);
boxTwo.removeClass('horizTranslate');
}
});
</script>
</body>
</html>
執行效果:http://cdpn.io/GokAm

同樣的技術可以用在更高級的方法上。下面的例子也是通過改變類名來觸發元素的transition(過渡),但這次可以跟蹤當前的縮放率。
<!DOCTYPE html>
<html>
<head>
<title>操作transtition</title>
<style type="text/css">
.zoomPic {
margin: 30px;
width: 300px;
height: 180px;
background-color: blue;
background-image: url(http://placehold.it/1200x720);
background-repeat:no-repeat;
background-position:50% 50%;
background-size: 300px 180px;

-webkit-transition: all 2.5s ease-in-out;
-moz-transition: all 2.5s ease-in-out;
-ms-transition: all 2.5s ease-in-out;
-o-transition: all 2.5s ease-in-out;
transition: all 2.5s ease-in-out;
}
.zoomPic.zoom {
background-size: 1200px 720px !important;
}
</style>
<script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>
<h3>Pure Javascript</h3>
<div class="zoomPic"></div>
<button class='zoom'>Zoom</button>
<button class='pause'>Pause</button>
<button class='zoomout'>Zoom Out</button>

<h3>jQuery</h3>
<div class='zoomPic'></div>
<button class='zoom'>Zoom</button>
<button class='pause'>Pause</button>
<button class='zoomout'>Zoom Out</button>

<script type="text/javascript">
var zoomOne = document.getElementsByClassName('zoomPic')[0],
zoomOneBgSize = window.getComputedStyle(zoomOne).getPropertyValue('background-size'),
zoomTwo = $(".zoomPic:eq(1)"),
zoomTwoBgSize = zoomTwo.css('background-size');
// zoomOne:zoom
document.getElementsByClassName('zoom')[0].onclick = function(){
if(!zoomOne.classList.contains('zoom')){
zoomOne.classList.add('zoom');
}
}
// zoomOne:pause
document.getElementsByClassName('pause')[0].onclick = function(){
var computedStyle = window.getComputedStyle(zoomOne),
backgroundSize = computedStyle.getPropertyValue("background-size");
zoomOne.style.backgroundSize = backgroundSize;
zoomOne.classList.remove('zoom');
}
// zoomOne:zoomout
document.getElementsByClassName('zoomout')[0].onclick = function(){
zoomOne.classList.remove('zoom');
zoomOne.style.backgroundSize = zoomOneBgSize;
}
// zoomTwo:zoom
$('.zoom:eq(1)').on('click',function(){
if(!zoomTwo.hasClass('zoom')){
zoomTwo.addClass('zoom');
}
});
// zoomTwo:pause
$('.pause:eq(1)').on('click',function(){
var computedStyle = zoomTwo.css('background-size');
zoomTwo.css('background-size',computedStyle);
zoomTwo.removeClass('zoom');
});
// zoomTwo:zoomout
$('.zoomout:eq(1)').on('click',function(){
zoomTwo.removeClass('zoom');
zoomTwo.css('background-size',zoomTwoBgSize);
});
</script>
</body>
</html>

轉載僅供參考,版權屬於原作者。祝你愉快,滿意請採納哦

『肆』 JS 怎麼動態設置CSS3動畫的樣式

引入jquery

然後給你要設置動畫的對象增加或者刪除css3動畫的類就可以了。

如我這里用colorchange這個漸變類在css裡面寫好動畫效果以後在js裡面給對象添加上就可以實現動畫了

<!DOCTYPEhtml>
<html>
<headlang="en">
<metacharset="UTF-8">
<title>Test</title>
<styletype="text/css">
body{
padding:20px;
background-color:#FFF;
}
.colorchange
{
animation:myfirst5s;
-moz-animation:myfirst5s;/*Firefox*/
-webkit-animation:myfirst5s;/*SafariandChrome*/
-o-animation:myfirst5s;/*Opera*/
}

@keyframesmyfirst
{
from{background:red;}
to{background:yellow;}
}

@-moz-keyframesmyfirst/*Firefox*/
{
from{background:red;}
to{background:yellow;}
}

@-webkit-keyframesmyfirst/*SafariandChrome*/
{
from{background:red;}
to{background:yellow;}
}

@-o-keyframesmyfirst/*Opera*/
{
from{background:red;}
to{background:yellow;}
}
#main{
width:100px;
height:100px;
background:red;
}
#cgbt{
width:100px;
margin:20px000;
text-align:center;
cursor:pointer;
}
#cgbt:hover{
background-color:#2D93CA;
}
</style>
</head>
<body>
<divid="main">
我會變么?
</div>
<divid="cgbt">
點我讓上面的變顏色
</div>
<scriptsrc="jquery-3.2.1.min.js"type="application/javascript"></script>
<script>
$(document).ready(function(){
$("#cgbt").click(function(){
$("#main").attr("class","colorchange");
});
});
</script>
</body>
</html>

『伍』 js+css如何實現動畫效果

簡單的不用js就行

<!DOCTYPEHTML>
<html>
<head>
<metacharset="utf8">
<title>untitled</title>
<linkrel="stylesheet"type="text/css"href="">
<styletype="text/css">
*{
margin:0px;
padding:0px;
}
#a{
position:absolute;
width:50px;
height:50px;
background-color:#f3e9e0;
border-radius:50%;
left:400px;
top:200px;
}
#adiv{
position:absolute;
width:50px;
height:50px;
border-radius:50%;
transition:all0.5s;
left:0px;
top:0px;
}
#a:nth-child(1){
background-color:#c1d4ed;
}
#a:nth-child(2){
background-color:#7d6e69;
}
#a:nth-child(3){
background-color:#dad6d5;
}
#a:nth-child(4){
background-color:#caaa9d;
}
#a:nth-child(5){
background-color:#6bdeff;
}
#a:hover:nth-child(1){
left:150px;
top:-150px;
}
#a:hover:nth-child(2){
left:150px;
top:150px;
}
#a:hover:nth-child(3){
left:300px;
top:-150px;
}
#a:hover:nth-child(4){
left:300px;
top:150px;
}
#a:hover:nth-child(5){
left:450px;
top:0px;
}
</style>
</head>
<body>
<divid='a'>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</body>
</html>

滑鼠伸到球上 自動擴散移動

『陸』 如何用js使得一個已經結束的css的animation動畫重新執行一遍

<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<metaname="viewport"content="width=device-width,initial-scale=1.0">
<metahttp-equiv="X-UA-Compatible"content="ie=edge">
<title>Document</title>
<style>
.box{width:400px;height:400px;background-color:red;}
.animation{
animation:rotate2sbothlinear;
}
@keyframesrotate{
form{
transform:rotate(0deg)
}
to{transform:rotate(360deg)}
}
</style>
</head>
<body>


<divclass="box">
box
</div>

<script>
document.addEventListener('DOMContentLoaded',function(){
varbox=document.querySelector('.box');
box.classList.add('animation');
box.addEventListener('animationend',function(e){
varself=this
self.classList.remove('animation')
setTimeout(function(){
self.classList.add('animation')
},1)
})
})
</script>

</body>
</html>

動畫播放結束的事件是animationend

如果你只是要無限循環的話,不用javascript,css animation這樣寫

<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<metaname="viewport"content="width=device-width,initial-scale=1.0">
<metahttp-equiv="X-UA-Compatible"content="ie=edge">
<title>Document</title>
<style>
.box{width:400px;height:400px;background-color:red;}
.animation{
animation:rotate2sbothlinearinfinite;
}
@keyframesrotate{
form{
transform:rotate(0deg)
}
to{transform:rotate(360deg)}
}
</style>
</head>
<body>


<divclass="boxanimation">
box
</div>


</body>
</html>

『柒』 使用CSS替代JS實現幾種常見的特效

可以代替JavaScript的css特效有很多,比如說通過偽類:hover、:actived等實現觸發效果,動畫效果可以用animation屬性,對圖像處理可以transform、filter等

『捌』 怎樣用js或者css來實現這種效果

<html>
<head>
<style>
</style>

<body>
<canvasid="emmm"width="233"height="233"></canvas>
</body>
<script>
varcanvas=document.getElementById('emmm');
varctx=canvas.getContext('2d');
img=newImage();
img.src="http://s6.sinaimg.cn/mw690/006xDASvzy73Xzra5sV55&690";
img.onload=function(){
ctx.drawImage(img,0,0);
ctx.globalCompositeOperation="destination-atop";
//canvas重疊屬性
//http://www.w3school.com.cn/tags/canvas_globalcompositeoperation.asp
ctx.font="bold50px微軟雅黑";
ctx.fillStyle="rgba(0,0,0,0);";
ctx.fillText("哈哈",50,100);
}
</script>
</head>
</html>
//哇一個問題發了兩遍看我能不能水兩個採納

閱讀全文

與用js實現css動畫效果相關的資料

熱點內容
電腦系統host文件 瀏覽:996
求一個在線播放的網站 瀏覽:596
四級丶四級電影﹥ 瀏覽:582
怎麼把cad的工具欄調出來 瀏覽:742
強奸了女僵屍的電影 瀏覽:15
能在線觀看最新網址 瀏覽:317
3d電影下載網站3d電影 瀏覽:261
華為手機如何把app弄成小窗口 瀏覽:589
flash的工具欄 瀏覽:106
古風sq片 瀏覽:705
如何挑選軟體編程培訓機構 瀏覽:873
巫師3112升級 瀏覽:163
zipjs怎麼用 瀏覽:619
手游天龍八部俠客升級 瀏覽:437
iphone5s蜂窩數據和3g有什麼區別 瀏覽:547
文件被病毒隱藏win10 瀏覽:710
主角重生到德國的小說 瀏覽:410
win10創意版本 瀏覽:436
韓國姜恩惠電影集合 瀏覽:436
無錫少兒編程哪裡好 瀏覽:779

友情鏈接