我在 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?
2 回答
子衿沉夜
TA贡献1828条经验 获得超3个赞
let banner = document.getElementById('#baner');确保这是正确的:
let banner = document.getElementById('banner');您不使用“#”,因为您已经说过它是一个 Id。
汪汪一只猫
TA贡献1898条经验 获得超8个赞
正如 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'
});
添加回答
举报
0/150
提交
取消
