为什么我不能删除背景附件属性?

时间:2019-06-03 01:46:44

标签: javascript html css media-queries

因此,我尝试通过最大宽度为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');
// }

3 个答案:

答案 0 :(得分:1)

您可以使用jquery删除背景

$("header").css("background-image", "none");

示例:

https://codepen.io/seyyedmojtaba72/pen/gJqLGE

答案 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';

或者将其设置为另一个特定值,类似于上面。