ล่าสุดการพัฒนาเว็บบทเรียน
×

JavaScript การอ้างอิง

ภาพรวม

JavaScript

JS เชือก JS จำนวน JS ผู้ประกอบการ JS งบ JS คณิตศาสตร์ JS วันที่ JS แถว JS บูลีน JS นิพจน์ทั่วไป JS สถานการณ์โดยรวม JS การแปลง

เบราว์เซอร์ BOM

Window Navigator Screen History Location

HTML DOM

DOM เอกสาร DOM องค์ประกอบ DOM แอตทริบิวต์ DOM เหตุการณ์ DOM รูปแบบ

HTML วัตถุ

<a> <abbr> <address> <area> <article> <aside> <audio> <b> <base> <bdo> <blockquote> <body> <br> <button> <canvas> <caption> <cite> <code> <col> <colgroup> <datalist> <dd> <del> <details> <dfn> <dialog> <div> <dl> <dt> <em> <embed> <fieldset> <figcaption> <figure> <footer> <form> <head> <header> <h1> - <h6> <hr> <html> <i> <iframe> <img> <ins> <input> button <input> checkbox <input> color <input> date <input> datetime <input> datetime-local <input> email <input> file <input> hidden <input> image <input> month <input> number <input> password <input> radio <input> range <input> reset <input> search <input> submit <input> text <input> time <input> url <input> week <kbd> <keygen> <label> <legend> <li> <link> <map> <mark> <menu> <menuitem> <meta> <meter> <nav> <object> <ol> <optgroup> <option> <output> <p> <param> <pre> <progress> <q> <s> <samp> <script> <section> <select> <small> <source> <span> <strong> <style> <sub> <summary> <sup> <table> <td> <th> <tr> <textarea> <time> <title> <track> <u> <ul> <var> <video>

วัตถุอื่น ๆ

CSSStyleDeclaration


 

HTML DOM scrollTop Propery

<ธาตุวัตถุ

ตัวอย่าง

รับจำนวนพิกเซลเนื้อหาของการ <div> องค์ประกอบเลื่อนในแนวนอนและแนวตั้ง:

var elmnt = document.getElementById("myDIV");
var x = elmnt.scrollLeft;
var y = elmnt.scrollTop;
ลองตัวเอง»

เพิ่มเติม "Try it Yourself" ตัวอย่างด้านล่าง


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

ชุดคุณสมบัติ scrollTop หรือส่งกลับจำนวนของพิกเซลเนื้อหาองค์ประกอบถูกเลื่อนในแนวตั้ง

เคล็ดลับ: ใช้ scrollLeft คุณสมบัติการตั้งค่าหรือกลับจำนวนพิกเซลเนื้อหาองค์ประกอบถูกเลื่อนในแนวนอน

เคล็ดลับ: การเพิ่มแถบเลื่อนไปยังองค์ประกอบให้ใช้แบบ CSS ล้น สถานที่ให้บริการ

แนะนำ: OnScroll เหตุการณ์เกิดขึ้นเมื่อเลื่อนเป็นองค์ประกอบที่จะถูกเลื่อน


สนับสนุนเบราว์เซอร์

คุณสมบัติ
scrollTop ใช่ ใช่ ใช่ ใช่ ใช่

วากยสัมพันธ์

กลับ scrollTop ทรัพย์สิน:

element .scrollTop

ตั้ง scrollTop ทรัพย์สิน:

element .scrollTop= pixels

ค่าทรัพย์สิน

ความคุ้มค่า ลักษณะ
pixels ระบุจำนวนของพิกเซลเนื้อหาขององค์ประกอบที่มีการเลื่อนในแนวตั้ง

บันทึกพิเศษ:
  • ถ้าตัวเลขเป็นค่าลบหมายเลขที่ถูกกำหนดเป็น "0"
  • หากองค์ประกอบไม่สามารถเลื่อนหมายเลขที่ถูกกำหนดเป็น "0"
  • ถ้าจำนวนมากกว่าจำนวนเงินเลื่อนสูงสุดที่อนุญาตจำนวนที่ถูกกำหนดเป็นจำนวนสูงสุด

รายละเอียดทางเทคนิค

กลับค่า: จำนวนคิดเป็นจำนวนพิกเซลที่เนื้อหาขององค์ประกอบที่ได้รับการเลื่อนแนวตั้ง

ตัวอย่าง

ตัวอย่างอื่น ๆ

ตัวอย่าง

เลื่อนเนื้อหาของการ <div> องค์ประกอบไปยัง 50 พิกเซลในแนวนอนและแนวตั้ง 10 พิกเซล:

var elmnt = document.getElementById("myDIV");
elmnt.scrollLeft = 50;
elmnt.scrollTop = 10;
ลองตัวเอง»

ตัวอย่าง

เลื่อนเนื้อหาของการ <div> องค์ประกอบโดย 50 พิกเซลในแนวนอนและแนวตั้ง 10 พิกเซล:

var elmnt = document.getElementById("myDIV");
elmnt.scrollLeft += 50;
elmnt.scrollTop += 10;
ลองตัวเอง»

ตัวอย่าง

เลื่อนเนื้อหาของ <body> 30 พิกเซลในแนวนอนและแนวตั้ง 10 พิกเซล:

var body = document.body; // For Chrome, Safari and Opera
var html = document.documentElement; // Firefox and IE places the overflow at the <html> level, unless else is specified. Therefore, we use the documentElement property for these two browsers
body.scrollLeft += 30;
body.scrollTop += 10;
html.scrollLeft += 30;
html.scrollTop += 10;
ลองตัวเอง»

ตัวอย่าง

สลับระหว่างชื่อชั้นบนเลื่อนตำแหน่งที่แตกต่างกัน - เมื่อผู้ใช้เลื่อนลง 50 พิกเซลจากด้านบนของหน้าชื่อชั้น "test" จะถูกเพิ่มองค์ประกอบ (and removed when scrolled up again)

window.onscroll = function() {myFunction()};

function myFunction() {
    if (document.body.scrollTop > 50 || document.documentElement.scrollTop > 50) {
        document.getElementById("myP").className = "test";
    } else {
        document.getElementById("myP").className = "";
    }
}
ลองตัวเอง»

ตัวอย่าง

เลื่อนในองค์ประกอบเมื่อผู้ใช้เลื่อนลง 350 พิกเซลจากด้านบนของหน้า (add the slideUp class) :

window.onscroll = function() {myFunction()};

function myFunction() {
    if (document.body.scrollTop > 350 || document.documentElement.scrollTop > 350) {
        document.getElementById("myImg").className = "slideUp";
}
ลองตัวเอง»

ตัวอย่าง

วาดรูปสามเหลี่ยมบนเลื่อน:

<!-- Use SVG to draw the triangle (has to be <path>) -->
<svg id="mySVG">
  <path fill="none" stroke="red" stroke-width="3" id="triangle" d="M150 0 L75 200 L225 200 Z">
</svg>

<script>
// Get the id of the <path> element and the length of <path>
var triangle = document.getElementById("triangle");
var length = triangle.getTotalLength();

// The start position of the drawing
triangle.style.strokeDasharray = length;

// Hide the triangle by offsetting dash. Remove this line to show the triangle before scroll draw
triangle.style.strokeDashoffset = length;

// Find scroll percentage on scroll (using cross-browser properties), and offset dash same amount as percentage scrolled
window.addEventListener("scroll", myFunction);

function myFunction() {
    var scrollpercent = (document.body.scrollTop + document.documentElement.scrollTop) / (document.documentElement.scrollHeight - document.documentElement.clientHeight);

    var draw = length * scrollpercent;

    // Reverse the drawing (when scrolling upwards)
    triangle.style.strokeDashoffset = length - draw;
}
</script>
ลองตัวเอง»


<ธาตุวัตถุ