[英]How to change jQuery css style to vanilla JS
我在 jQuery 中有一個代碼,我需要將其轉換為純 javascript 代碼。
$(window).scroll(function () {
$('.baner').css({'transform':'translate3d(0px, '+(-($(this).scrollTop()/5))+"px"+', 0px'});
});
我試圖做的是:
let win = document.querySelector(window); let banner = document.getElementById('#baner'); win.addEventListener('scroll',function () { banner.css({'transform':'translate3d(0px, '+(-($(this).scrollTop()/5))+"px"+', 0px'}); });
<div class='#baner'> </div>
如何更改 jquery css 代碼.css({'transform':'translate3d(0px, '+(-($(this).scrollTop()/5))+"px"+', 0px'});
JS?
let banner = document.getElementById('#baner');
確保這是正確的:
let banner = document.getElementById('banner');
您不使用“#”,因為您已經說過它是一個 ID。
正如 Kalinauskas 在他的回答中提到的,你可以使用 document.getElementById()。
// () => {} is ES6 shorthand for a function (also called arrow function, read about it here https://www.w3schools.com/js/js_arrow_function.asp
// Instead of the .css function for jQuery, you have .style.yourStyleAtributeHere
let banner = document.getElementById('baner');
window.addEventListener("scroll", () => {
banner.style.transform = 'translate3d(0px, ' + (-(banner.scrollTop / 5))+"px"+', 0px'
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.