jQueryアニメーション-サイズのスムーズな移行
質問
これは非常に簡単かもしれませんが、まだ学ぶべき例が見つからないので、ご容赦ください。 ;)
基本的に私がやりたいことは次のとおりです。
<div>Lots of content! Lots of content! Lots of content! ...</div>
....
$("div").html("Itsy-bitsy bit of content!");
新しいコンテンツが挿入されたときに、多くのコンテンツを含むdivのディメンションと非常に小さなdivのディメンションの間をスムーズにアニメーション化したい。
思考?
解決
このjQueryプラグインを試してください:
// Animates the dimensional changes resulting from altering element contents
// Usage examples:
// $("#myElement").showHtml("new HTML contents");
// $("div").showHtml("new HTML contents", 400);
// $(".className").showHtml("new HTML contents", 400,
// function() {/* on completion */});
(function($)
{
$.fn.showHtml = function(html, speed, callback)
{
return this.each(function()
{
// The element to be modified
var el = $(this);
// Preserve the original values of width and height - they'll need
// to be modified during the animation, but can be restored once
// the animation has completed.
var finish = {width: this.style.width, height: this.style.height};
// The original width and height represented as pixel values.
// These will only be the same as `finish` if this element had its
// dimensions specified explicitly and in pixels. Of course, if that
// was done then this entire routine is pointless, as the dimensions
// won't change when the content is changed.
var cur = {width: el.width()+'px', height: el.height()+'px'};
// Modify the element's contents. Element will resize.
el.html(html);
// Capture the final dimensions of the element
// (with initial style settings still in effect)
var next = {width: el.width()+'px', height: el.height()+'px'};
el .css(cur) // restore initial dimensions
.animate(next, speed, function() // animate to final dimensions
{
el.css(finish); // restore initial style settings
if ( $.isFunction(callback) ) callback();
});
});
};
})(jQuery);
Commenter RonLuggeは、同じ要素で2回呼び出すと問題が発生する可能性があることを指摘しています。最初のアニメーションは2番目のアニメーションが始まる前に終了していません。これは、2番目のアニメーションが現在の(アニメーション中の)サイズを希望の「終了」として取るためです。値を最終値として修正します(「自然な」サイズに向かってアニメーション化するのではなく、トラック内のアニメーションを効果的に停止します)...
これを解決する最も簡単な方法は、 stop()
を呼び出すことです。 showHtml()
を呼び出し、2番目の( jumpToEnd )パラメーターに true
を渡す前:
$(selector).showHtml("new HTML contents")
.stop(true, true)
.showHtml("even newer contents");
これにより、新しいアニメーションを開始する前に、最初のアニメーションがすぐに完了します(まだ実行中の場合)。
他のヒント
animateメソッドを使用できます。
$("div").animate({width:"200px"},400);
おそらくこのようなものですか?
$(".testLink").click(function(event) {
event.preventDefault();
$(".testDiv").hide(400,function(event) {
$(this).html("Itsy-bitsy bit of content!").show(400);
});
});
あなたが望むと思うものに近い、slideIn / slideOutを試すか、UI / Effectsプラグインを見てください。
これは私がこれを修正した方法です、これが役に立つことを願っています! アニメーションは100%スムーズです:)
HTML:
<div id="div-1"><div id="div-2">Some content here</div></div>
Javascript:
// cache selectors for better performance
var container = $('#div-1'),
wrapper = $('#div-2');
// temporarily fix the outer div's width
container.css({width: wrapper.width()});
// fade opacity of inner div - use opacity because we cannot get the width or height of an element with display set to none
wrapper.fadeTo('slow', 0, function(){
// change the div content
container.html("<div id=\"2\" style=\"display: none;\">new content (with a new width)</div>");
// give the outer div the same width as the inner div with a smooth animation
container.animate({width: wrapper.width()}, function(){
// show the inner div
wrapper.fadeTo('slow', 1);
});
});
コードの短いバージョンがあるかもしれませんが、私はこのように保ちました。
これは私の仕事です。一時divに幅を追加することもできます。
$('div#to-transition').wrap( '<div id="tmp"></div>' );
$('div#tmp').css( { height: $('div#to-transition').outerHeight() + 'px' } );
$('div#to-transition').fadeOut('fast', function() {
$(this).html(new_html);
$('div#tmp').animate( { height: $(this).outerHeight() + 'px' }, 'fast' );
$(this).fadeIn('fast', function() {
$(this).unwrap();
});
});
こんにちはmeyahoocoma4c5ki0pprxr19sxhajsogo6jgks5dt。
「content div」は、絶対幅の値に設定された「outer div」でラップできます。 &quot; hide()&quot;で新しいコンテンツを挿入しますまたは&quot; animate({width})&quot;他の回答に示されている方法。この方法では、ラッパーdivが一定の幅を保持するため、ページはその間にリフローしません。
dequeue
を使用して、jQueryアニメーションをスムーズにできます。新しいアニメーションを開始する前に、クラスの存在をテストします(ホバー時に設定され、mouseOutアニメーションコールバックで削除されます)。新しいアニメーションが開始したら、デキューします。
var space = ($(window).width() - 100);
$('.column').width(space/4);
$(".column").click(function(){
if (!$(this).hasClass('animated')) {
$('.column').not($(this).parent()).dequeue().stop().animate({width: 'toggle', opacity: '0.75'}, 1750,'linear', function () {});
}
$(this).addClass('animated');
$('.column').not($(this).parent()).dequeue().stop().animate({width: 'toggle', opacity: '0.75'}, 1750,'linear', function () {
$(this).removeClass('animated').dequeue();
});
$(this).dequeue().stop().animate({
width:(space/4)
}, 1400,'linear',function(){
$(this).html('AGAIN');
});
});
デモは5つのフルハイト列として設定されています。列2から5のいずれかをクリックすると、他の3つの幅が切り替わり、クリックした要素が左端に移動します。
jqueryプラグインソリューションに便乗するには(これをコメントとして追加するには評判が低すぎる)jQuery.html()は、追加されたhtmlのイベントハンドラーを削除します。変更:
// Modify the element's contents. Element will resize.
el.html(html);
to
// Modify the element's contents. Element will resize.
el.append(html);
&quot; html&quot;のイベントハンドラを保持します要素