2017-06-22 86 views

回答

2

,可以嘗試通過CSS加入一些transition動畫如下,並添加/刪除類說small這裏定義小widthleft性能:

.child-element { 
    width: 90%; 
    overflow: auto; 
    margin: auto; 
    position: absolute; 
    top: 0px; 
    left: 0; 
    bottom: 0; 
    right: 0; 
    -webkit-transition: all 1s ease-in-out; 
    -moz-transition: all 1s ease-in-out; 
    -o-transition: all 1s ease-in-out; 
    transition: all 1s ease-in-out; 
} 

.small { 
    left: 20px; 
    width: 80%; 
} 

JS

$('#makesmall').on('click', function() { 
    $('.child-element').addClass('small'); 
}); 


$('#makebig').on('click', function() { 
    $('.child-element').removeClass('small'); 
}); 

UPDATED FIDDLE

0

無論如何,動畫寬度和定位都不是高性能的。你可以嘗試通過jquery添加一個類,並使用translate 3d來開始一個css動畫。或添加

-webkit-transform: translate3d(0, 0, 0); 
    -moz-transform: translate3d(0, 0, 0); 
    -ms-transform: translate3d(0, 0, 0); 
    transform: translate3d(0, 0, 0); 
    /* Other transform properties here */ 

到類

http://blog.teamtreehouse.com/increase-your-sites-performance-with-hardware-accelerated-css

+0

我需要將它添加到我的孩子元素類或我需要建立一個新的類? –

2

使用CSS轉換。

.child-element{ transition: width 600ms, left 600ms } 
.child-element.small{width: 80%; left: 20px;} 
.child-element.big{width: 90%; left: 0;} 

$('#makebig').on('click', function(){ 
    $('.child-element').removeClass("small"); 
    $('.child-element').addClass("big"); 
}); 
$('#makesmall').on('click', function(){ 
    $('.child-element').removeClass("big"); 
    $('.child-element').addClass("small");}); 
} 
2

使用addClassremoveClass當點擊

.animate { 
    width: 100% !important; 
    left: 20px !important; 
    transition: all 0.5s; 
} 

JS

$('#makesmall').on('click', function(){ 
    $('.child-element').addClass('animate'); 
}); 


$('#makebig').on('click', function(){ 
    $('.child-element').removeClass('animate'); 
});