كيف - التمرير السلس
تعرف على كيفية إنشاء تأثير تمرير سلس باستخدام CSS.
التمرير السلس
القسم 1
انقر فوق الارتباط لمشاهدة تأثير التمرير "السلس".
انقر فوق Me للتمرير السلس إلى القسم 2 أدناهملاحظة: قم بإزالة خاصية سلوك التمرير لإزالة التمرير المتجانس.
التمرير السلس
أضف scroll-behavior: smooth
إلى عنصر <html> لتمكين التمرير السلس للصفحة بأكملها (ملاحظة: من الممكن أيضًا إضافته إلى عنصر / حاوية تمرير معينة):
مثال
html {
scroll-behavior: smooth;
}
دعم المتصفح
تحدد الأرقام الواردة في الجدول إصدار المستعرض الأول الذي يدعم خاصية سلوك التمرير بشكل كامل.
Property | |||||
---|---|---|---|---|---|
scroll-behavior | 61.0 | 79.0 | 36.0 | 14.0 | 48.0 |
عبر متصفح الحل
بالنسبة إلى المتصفحات التي لا تدعم scroll-behavior
الخاصية ، يمكنك استخدام JavaScript أو مكتبة JavaScript ، مثل jQuery ، لإنشاء حل يعمل مع جميع المتصفحات:
مثال
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
// Add smooth scrolling to all
links
$("a").on('click', function(event) {
// Make sure this.hash
has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();
//
Store hash
var hash = this.hash;
// Using jQuery's animate() method
to add smooth page scroll
// The optional number (800) specifies the number
of milliseconds it takes to scroll to the specified area
$('html,
body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
});
</script>
نصيحة: اقرأ المزيد حول خاصية سلوك التمرير في مرجع CSS الخاص بنا: خاصية سلوك التمرير CSS .