แอปพลิเคชันของฉันทำงานได้ไม่ดีกับ slideDown และ slideUp ของ jQuery ฉันต้องการใช้ CSS3 ที่เทียบเท่าในเบราว์เซอร์ที่รองรับ
เป็นไปได้ไหมโดยใช้การเปลี่ยน CSS3 เพื่อเปลี่ยนองค์ประกอบจากdisplay: none;ไปเป็นdisplay: block;ในขณะที่เลื่อนรายการลงหรือขึ้น
แอปพลิเคชันของฉันทำงานได้ไม่ดีกับ slideDown และ slideUp ของ jQuery ฉันต้องการใช้ CSS3 ที่เทียบเท่าในเบราว์เซอร์ที่รองรับ
เป็นไปได้ไหมโดยใช้การเปลี่ยน CSS3 เพื่อเปลี่ยนองค์ประกอบจากdisplay: none;ไปเป็นdisplay: block;ในขณะที่เลื่อนรายการลงหรือขึ้น
คำตอบ:
คุณสามารถทำสิ่งนี้:
#youritem .fade.in {
animation-name: fadeIn;
}
#youritem .fade.out {
animation-name: fadeOut;
}
@keyframes fadeIn {
0% {
opacity: 0;
transform: translateY(startYposition);
}
100% {
opacity: 1;
transform: translateY(endYposition);
}
}
@keyframes fadeOut {
0% {
opacity: 1;
transform: translateY(startYposition);
}
100% {
opacity: 0;
transform: translateY(endYposition);
}
}
ตัวอย่าง - Slide and Fade:
สิ่งนี้จะเลื่อนและเคลื่อนไหวความทึบ - ไม่ได้ขึ้นอยู่กับความสูงของคอนเทนเนอร์ แต่อยู่ด้านบน / พิกัด ดูตัวอย่าง
ตัวอย่าง - ความสูงอัตโนมัติ / ไม่มี Javascript: นี่คือตัวอย่างสดไม่ต้องการความสูง - จัดการกับความสูงอัตโนมัติและไม่ต้องใช้จาวาสคริปต์
ดูตัวอย่าง
ฉันเปลี่ยนโซลูชันของคุณเพื่อให้ใช้งานได้กับเบราว์เซอร์สมัยใหม่ทั้งหมด:
ข้อมูลโค้ด css:
-webkit-transition: height 1s ease-in-out;
-moz-transition: height 1s ease-in-out;
-ms-transition: height 1s ease-in-out;
-o-transition: height 1s ease-in-out;
transition: height 1s ease-in-out;
ข้อมูลโค้ด js:
var clone = $('#this').clone()
.css({'position':'absolute','visibility':'hidden','height':'auto'})
.addClass('slideClone')
.appendTo('body');
var newHeight = $(".slideClone").height();
$(".slideClone").remove();
$('#this').css('height',newHeight + 'px');
นี่คือตัวอย่างเต็มhttp://jsfiddle.net/RHPQd/
-2นี้หลังจาก 7 ปียังคงทำงานอย่างมีเสน่ห์ ยกเว้นว่าฉันจะสร้างสิ่งนี้ขึ้นมาใหม่ด้วย Vanilla JS +1 จากฉัน.
ดังนั้นฉันจึงตอบคำถามของตัวเองต่อไป :)
คำตอบของ @ True ถือเป็นการเปลี่ยนองค์ประกอบให้มีความสูงเฉพาะ ปัญหานี้คือฉันไม่รู้ความสูงขององค์ประกอบ (มันอาจผันผวนได้)
ฉันพบวิธีแก้ปัญหาอื่น ๆ ซึ่งใช้ความสูงสูงสุดเป็นตัวเปลี่ยน แต่สิ่งนี้สร้างภาพเคลื่อนไหวที่กระตุกมากสำหรับฉัน
โซลูชันของฉันด้านล่างใช้ได้เฉพาะในเบราว์เซอร์ WebKit
แม้ว่าจะไม่ใช่ CSS ล้วนๆ แต่ก็เกี่ยวข้องกับการเปลี่ยนความสูงซึ่งกำหนดโดย JS บางตัว
$('#click-me').click(function() {
var height = $("#this").height();
if (height > 0) {
$('#this').css('height', '0');
} else {
$("#this").css({
'position': 'absolute',
'visibility': 'hidden',
'height': 'auto'
});
var newHeight = $("#this").height();
$("#this").css({
'position': 'static',
'visibility': 'visible',
'height': '0'
});
$('#this').css('height', newHeight + 'px');
}
});
#this {
width: 500px;
height: 0;
max-height: 9999px;
overflow: hidden;
background: #BBBBBB;
-webkit-transition: height 1s ease-in-out;
}
#click-me {
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<p id="click-me">click me</p>
<div id="this">here<br />is<br />a<br />bunch<br />of<br />content<br />sdf</div>
<div>always shows</div>
ทำไมไม่ใช้ประโยชน์จากการเปลี่ยน css ของเบราว์เซอร์ที่ทันสมัยและทำให้สิ่งต่างๆง่ายขึ้นและรวดเร็วโดยใช้ css มากขึ้นและ jquery น้อยลง
นี่คือรหัสสำหรับเลื่อนขึ้นและลง
นี่คือรหัสสำหรับเลื่อนจากซ้ายไปขวา
ในทำนองเดียวกันเราสามารถเปลี่ยนการเลื่อนจากบนลงล่างหรือขวาไปซ้ายโดยการเปลี่ยนแปลงกำเนิดและการแปลง: scaleX (0) หรือ transform: scaleY (0) อย่างเหมาะสม
การเปลี่ยนความสูงในการทำงานอาจเป็นเรื่องยุ่งยากเล็กน้อยเนื่องจากคุณต้องทราบความสูงที่จะทำให้เคลื่อนไหวได้ สิ่งนี้มีความซับซ้อนมากขึ้นโดยการเติมในองค์ประกอบให้เคลื่อนไหว
นี่คือสิ่งที่ฉันคิดขึ้น:
ใช้สไตล์เช่นนี้:
.slideup, .slidedown {
max-height: 0;
overflow-y: hidden;
-webkit-transition: max-height 0.8s ease-in-out;
-moz-transition: max-height 0.8s ease-in-out;
-o-transition: max-height 0.8s ease-in-out;
transition: max-height 0.8s ease-in-out;
}
.slidedown {
max-height: 60px ; // fixed width
}
รวมเนื้อหาของคุณไว้ในคอนเทนเนอร์อื่นเพื่อให้คอนเทนเนอร์ที่คุณเลื่อนไม่มีช่องว่าง / ระยะขอบ / ขอบ:
<div id="Slider" class="slideup">
<!-- content has to be wrapped so that the padding and
margins don't effect the transition's height -->
<div id="Actual">
Hello World Text
</div>
</div>
จากนั้นใช้สคริปต์บางส่วน (หรือมาร์กอัปที่เปิดเผยในเฟรมเวิร์กการผูก) เพื่อทริกเกอร์คลาส CSS
$("#Trigger").click(function () {
$("#Slider").toggleClass("slidedown slideup");
});
ตัวอย่างที่นี่: http://plnkr.co/edit/uhChl94nLhrWCYVhRBUF?p=preview
ใช้งานได้ดีสำหรับเนื้อหาขนาดคงที่ สำหรับการละลายทั่วไปคุณสามารถใช้รหัสเพื่อหาขนาดขององค์ประกอบเมื่อเปิดใช้งานการเปลี่ยนแปลง ต่อไปนี้เป็นปลั๊กอิน jQuery ที่ทำเช่นนั้น:
$.fn.slideUpTransition = function() {
return this.each(function() {
var $el = $(this);
$el.css("max-height", "0");
$el.addClass("height-transition-hidden");
});
};
$.fn.slideDownTransition = function() {
return this.each(function() {
var $el = $(this);
$el.removeClass("height-transition-hidden");
// temporarily make visible to get the size
$el.css("max-height", "none");
var height = $el.outerHeight();
// reset to 0 then animate with small delay
$el.css("max-height", "0");
setTimeout(function() {
$el.css({
"max-height": height
});
}, 1);
});
};
ซึ่งสามารถเรียกได้ดังนี้:
$ ("# Trigger") คลิก (function () {
if ($("#SlideWrapper").hasClass("height-transition-hidden"))
$("#SlideWrapper").slideDownTransition();
else
$("#SlideWrapper").slideUpTransition();
});
กับมาร์กอัปเช่นนี้:
<style>
#Actual {
background: silver;
color: White;
padding: 20px;
}
.height-transition {
-webkit-transition: max-height 0.5s ease-in-out;
-moz-transition: max-height 0.5s ease-in-out;
-o-transition: max-height 0.5s ease-in-out;
transition: max-height 0.5s ease-in-out;
overflow-y: hidden;
}
.height-transition-hidden {
max-height: 0;
}
</style>
<div id="SlideWrapper" class="height-transition height-transition-hidden">
<!-- content has to be wrapped so that the padding and
margins don't effect the transition's height -->
<div id="Actual">
Your actual content to slide down goes here.
</div>
</div>
ตัวอย่าง: http://plnkr.co/edit/Wpcgjs3FS4ryrhQUAOcU?p=preview
ฉันเพิ่งเขียนสิ่งนี้ในโพสต์บล็อกหากคุณสนใจรายละเอียดเพิ่มเติม:
http://weblog.west-wind.com/posts/2014/Feb/22/Using-CSS-Transitions-to-SlideUp-and-SlideDown
document.getElementById("Slider").classList.toggle("slidedown");
ฉันขอแนะนำให้ใช้jQuery Transit Pluginซึ่งใช้คุณสมบัติการแปลง CSS3 ซึ่งใช้งานได้ดีบนอุปกรณ์พกพาเนื่องจากส่วนใหญ่รองรับการเร่งด้วยฮาร์ดแวร์เพื่อให้รูปลักษณ์ดั้งเดิม
HTML:
<div class="moveMe">
<button class="moveUp">Move Me Up</button>
<button class="moveDown">Move Me Down</button>
<button class="setUp">Set Me Up</button>
<button class="setDown">Set Me Down</button>
</div>
Javascript:
$(".moveUp").on("click", function() {
$(".moveMe").transition({ y: '-=5' });
});
$(".moveDown").on("click", function() {
$(".moveMe").transition({ y: '+=5' });
});
$(".setUp").on("click", function() {
$(".moveMe").transition({ y: '0px' });
});
$(".setDown").on("click", function() {
$(".moveMe").transition({ y: '200px' });
});
slideUp()and slideDown()method ด้วยปลั๊กอินนี้ คงจะดีจริงๆ
หลังจากการวิจัยและการทดลองฉันคิดว่าแนวทางที่ดีที่สุดคือการมีความสูง0pxและปล่อยให้มันเปลี่ยนเป็นความสูงที่แน่นอน คุณจะได้ความสูงที่แน่นอนด้วย JavaScript JavaScript ไม่ได้ทำการเคลื่อนไหว แต่เพียงแค่เปลี่ยนค่าความสูงเท่านั้น ตรวจสอบ:
function setInfoHeight() {
$(window).on('load resize', function() {
$('.info').each(function () {
var current = $(this);
var closed = $(this).height() == 0;
current.show().height('auto').attr('h', current.height() );
current.height(closed ? '0' : current.height());
});
});
เมื่อใดก็ตามที่หน้าโหลดหรือปรับขนาดองค์ประกอบที่มีคลาสinfoจะได้รับการhอัปเดตแอตทริบิวต์ แล้วคุณอาจจะมีการเรียกปุ่มstyle="height: __"ตั้งค่าให้ที่ตั้งก่อนหน้านี้hค่า
function moreInformation() {
$('.icon-container').click(function() {
var info = $(this).closest('.dish-header').next('.info'); // Just the one info
var icon = $(this).children('.info-btn'); // Select the logo
// Stop any ongoing animation loops. Without this, you could click button 10
// times real fast, and watch an animation of the info showing and closing
// for a few seconds after
icon.stop();
info.stop();
// Flip icon and hide/show info
icon.toggleClass('flip');
// Metnod 1, animation handled by JS
// info.slideToggle('slow');
// Method 2, animation handled by CSS, use with setInfoheight function
info.toggleClass('active').height(icon.is('.flip') ? info.attr('h') : '0');
});
};
นี่คือสไตล์สำหรับinfoชั้นเรียน
.info {
display: inline-block;
height: 0px;
line-height: 1.5em;
overflow: hidden;
padding: 0 1em;
transition: height 0.6s, padding 0.6s;
&.active {
border-bottom: $thin-line;
padding: 1em;
}
}
ฉันใช้สิ่งนี้กับหนึ่งในโปรเจ็กต์ของฉันดังนั้นชื่อคลาสจึงเฉพาะเจาะจง คุณสามารถเปลี่ยนแปลงได้ตามที่คุณต้องการ
รูปแบบอาจไม่รองรับการใช้งานข้ามเบราว์เซอร์ ทำงานได้ดีใน Chrome
ด้านล่างนี้เป็นตัวอย่างจริงสำหรับรหัสนี้ เพียงคลิกที่?ไอคอนเพื่อเริ่มภาพเคลื่อนไหว
ตัวแปรที่ไม่มี JavaScript CSS เท่านั้น
CSS:
.toggle_block {
border: 1px solid #ccc;
text-align: left;
background: #fff;
overflow: hidden;
}
.toggle_block .toggle_flag {
display: block;
width: 1px;
height: 1px;
position: absolute;
z-index: 0;
left: -1000px;
}
.toggle_block .toggle_key {
font-size: 16px;
padding: 10px;
cursor: pointer;
-webkit-transition: all 300ms ease;
-moz-transition: all 300ms ease;
-ms-transition: all 300ms ease;
-o-transition: all 300ms ease;
transition: all 300ms ease;
}
.toggle_block .content {
padding: 0 10px;
overflow: hidden;
max-height: 0;
-webkit-transition: all 300ms ease;
-moz-transition: all 300ms ease;
-ms-transition: all 300ms ease;
-o-transition: all 300ms ease;
transition: all 300ms ease;
}
.toggle_block .content .toggle_close {
cursor: pointer;
font-size: 12px;
}
.toggle_block .toggle_flag:checked ~ .toggle_key {
background: #dfd;
}
.toggle_block .toggle_flag:checked ~ .content {
max-height: 1000px;
padding: 10px 10px;
}
HTML:
<div class="toggle_block">
<input type="checkbox" id="toggle_1" class="toggle_flag">
<label for="toggle_1" class="toggle_key">clicker</label>
<div class="content">
Text 1<br>
Text 2<br>
<label for="toggle_1" class="toggle_close">close</label>
</div>
</div>
สำหรับบล็อกถัดไปให้เปลี่ยนเฉพาะ ID และแอตทริบิวต์ FOR ใน html
คุณไม่สามารถเลื่อนสไลด์อัพได้อย่างง่ายดายด้วย css3 เพราะเหตุใดฉันจึงเปลี่ยนสคริปต์ JensT เป็นปลั๊กอินที่มีทางเลือกสำรองและการโทรกลับของจาวาสคริปต์
ด้วยวิธีนี้หากคุณมี brwowser ที่ทันสมัยคุณสามารถใช้ css3 csstransition หากเบราว์เซอร์ของคุณไม่รองรับอย่างสง่างามให้ใช้ slideDown slideUp แบบเก่า
/* css */
.csstransitions .mosneslide {
-webkit-transition: height .4s ease-in-out;
-moz-transition: height .4s ease-in-out;
-ms-transition: height .4s ease-in-out;
-o-transition: height .4s ease-in-out;
transition: height .4s ease-in-out;
max-height: 9999px;
overflow: hidden;
height: 0;
}
ปลั๊กอิน
(function ($) {
$.fn.mosne_slide = function (
options) {
// set default option values
defaults = {
delay: 750,
before: function () {}, // before callback
after: function () {} // after callback;
}
// Extend default settings
var settings = $.extend({},
defaults, options);
return this.each(function () {
var $this = $(this);
//on after
settings.before.apply(
$this);
var height = $this.height();
var width = $this.width();
if (Modernizr.csstransitions) {
// modern browsers
if (height > 0) {
$this.css(
'height',
'0')
.addClass(
"mosne_hidden"
);
} else {
var clone =
$this.clone()
.css({
'position': 'absolute',
'visibility': 'hidden',
'height': 'auto',
'width': width
})
.addClass(
'mosne_slideClone'
)
.appendTo(
'body'
);
var newHeight =
$(
".mosne_slideClone"
)
.height();
$(
".mosne_slideClone"
)
.remove();
$this.css(
'height',
newHeight +
'px')
.removeClass(
"mosne_hidden"
);
}
} else {
//fallback
if ($this.is(
":visible"
)) {
$this.slideUp()
.addClass(
"mosne_hidden"
);
} else {
$this.hide()
.slideDown()
.removeClass(
"mosne_hidden"
);
}
}
//on after
setTimeout(function () {
settings.after
.apply(
$this
);
}, settings.delay);
});
}
})(jQuery);;
วิธีการใช้งาน
/* jQuery */
$(".mosneslide").mosne_slide({
delay:400,
before:function(){console.log("start");},
after:function(){console.log("done");}
});
คุณสามารถดูหน้าสาธิตได้ที่นี่ http://www.mosne.it/playground/mosne_slide_up_down/
try this for slide up slide down with animation
give your **height
@keyframes slide_up{
from{
min-height: 0;
height: 0px;
opacity: 0;
}
to{
height: 560px;
opacity: 1;
}
}
@keyframes slide_down{
from{
height: 560px;
opacity: 1;
}
to{
min-height: 0;
height: 0px;
opacity: 0;
}
}