HTML DOM scrollTop الخاصية
مثال
احصل على عدد البكسل حيث يتم تمرير محتوى عنصر <div> أفقيًا وعموديًا:
var elmnt = document.getElementById("myDIV");
var x = elmnt.scrollLeft;
var y = elmnt.scrollTop;
المزيد من الأمثلة "جربها بنفسك" أدناه.
التعريف والاستخدام
تحدد خاصية scrollTop أو تُرجع عدد وحدات البكسل التي يتم تمرير محتوى العنصر فيها عمودياً.
تلميح: استخدم خاصية scrollLeft لتعيين أو إرجاع عدد وحدات البكسل التي يتم تمرير محتوى العنصر فيها أفقيًا.
نصيحة: لإضافة أشرطة التمرير إلى عنصر ، استخدم خاصية CSS overflow .
تلميح: يقع حدث onscroll عندما يتم تمرير شريط تمرير أحد العناصر.
دعم المتصفح
Property | |||||
---|---|---|---|---|---|
scrollTop | Yes | Yes | Yes | Yes | Yes |
بناء الجملة
قم بإرجاع خاصية scrollTop:
element.scrollTop
قم بتعيين خاصية scrollTop:
element.scrollTop = pixels
قيم الملكية
Value | Description |
---|---|
pixels |
Specifies the number of pixels the element's content is scrolled vertically. Special notes:
|
تفاصيل تقنية
قيمة الإرجاع: | رقم ، يمثل عدد وحدات البكسل التي تم تمرير محتوى العنصر إليها عموديًا |
---|
مزيد من الأمثلة
مثال
قم بتمرير محتويات عنصر <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; // Safari
var html = document.documentElement; //
Chrome, Firefox, IE and Opera places the overflow at the <html> level, unless else is specified. Therefore, we use the
documentElement property for these browsers
body.scrollLeft += 30;
body.scrollTop += 10;
html.scrollLeft += 30;
html.scrollTop += 10;
مثال
التبديل بين أسماء الفئات في مواضع تمرير مختلفة - عندما يقوم المستخدم بالتمرير لأسفل بمقدار 50 بكسل من أعلى الصفحة ، ستتم إضافة اسم الفئة "اختبار" إلى عنصر (وإزالته عند التمرير لأعلى مرة أخرى).
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 بكسل من أعلى الصفحة (أضف فئة slideUp):
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>