因此,我尝试通过最大宽度为1024px的媒体查询删除背景附件属性。我只是通过background-attachment: none
来执行此操作,除了我的devtools闪烁错误并且该属性是否被划掉了?有什么想法吗?
https://jsfiddle.net/dfwg2nbv/1/
const ham = document.querySelector('.ham-menu');
const nav = document.querySelector('nav');
const header = document.querySelector('header');
const promise = document.querySelector('.promise');
const services = document.querySelector('.services');
const testimony = document.querySelector('.testimony');
header.style.removeProperty('background-attachment');
//detect mobile
// if ("ontouchstart" in document.documentElement) {
// removeProps(header);
// removeProps(promise);
// removeProps(services);
// removeProps(testimony);
// }
ham.addEventListener('click', animateMenu);
function animateMenu() {
nav.classList.toggle('hamburger-open');
}
// function removeProps(node) {
// node.style.removeProperty('background-attachment');
// node.style.removeProperty('background-size');
// }
答案 0 :(得分:1)
您可以使用jquery删除背景
$("header").css("background-image", "none");
示例:
答案 1 :(得分:0)
您正试图修改样式对象(该样式对象仅反映通过style
属性设置的样式,而不反映通过样式表设置的样式),因此未设置该样式对象以将其删除。您可以通过将其设置回default value of scroll
来覆盖它。
header.style.backgroundAttachment='scroll';
答案 2 :(得分:0)
none
不是background-attachment
属性的有效值。
此外,您也不怎么使用removeProperty
。这不是style
的方法。
您可以做的是将其设置为空白值,以尝试从内联样式中删除该属性,例如:
header.style.backgroundAttachment = '';
或者,如果您在样式表中进行了其他设置,则可以将其设置为初始值,例如:
header.style.backgroundAttachment = 'initial';
或者将其设置为另一个特定值,类似于上面。