jQuery toggle()วิธีการ

❮ วิธีการเอฟเฟกต์ jQuery

ตัวอย่าง

สลับไปมาระหว่างซ่อนและแสดงสำหรับองค์ประกอบ <p> ทั้งหมด:

$("button").click(function(){
  $("p").toggle();
});

ความหมายและการใช้งาน

วิธี toggle() สลับไปมาระหว่างhide()และ show()สำหรับองค์ประกอบที่เลือก

วิธีนี้จะตรวจสอบองค์ประกอบที่เลือกเพื่อให้มองเห็นได้ show() จะทำงานหากองค์ประกอบถูกซ่อน hide() จะทำงานหากองค์ประกอบมองเห็นได้ - สิ่งนี้จะสร้างเอฟเฟกต์การสลับ

หมายเหตุ : องค์ประกอบที่ซ่อนอยู่จะไม่ปรากฏเลย (ไม่ส่งผลต่อเค้าโครงของหน้าอีกต่อไป)

เคล็ดลับ:วิธีนี้สามารถใช้เพื่อสลับระหว่างฟังก์ชันที่กำหนดเองได้


ไวยากรณ์

$(selector).toggle(speed,easing,callback)

Parameter Description
speed Optional. Specifies the speed of the hide/show effect

Possible values:

  • milliseconds
  • "slow"
  • "fast"
easing Optional. Specifies the speed of the element in different points of the animation. Default value is "swing"

Possible values:

  • "swing" - moves slower at the beginning/end, but faster in the middle
  • "linear" - moves in a constant speed
Tip: More easing functions are available in external plugins
callback Optional. A function to be executed after the toggle() method is completed

To learn more about callback, visit our jQuery Callback chapter


ลองด้วยตัวคุณเอง - ตัวอย่าง


วิธีใช้พารามิเตอร์ความเร็วเพื่อระบุความเร็วของเอฟเฟกต์ซ่อน/แสดง


วิธีการใช้พารามิเตอร์ callback เมื่อสลับไปมาระหว่างเอฟเฟกต์ซ่อน/แสดง


❮ วิธีการเอฟเฟกต์ jQuery