简体   繁体   English

使用jQuery删除overflow-x和overflow-y

[英]Remove overflow-x and overflow-y using jQuery

I want to remove overflow-x and overflow-y using jQuery. 我想使用jQuery删除overflow-xoverflow-y I have tried several methods, but they don't seem to work: 我尝试了几种方法,但它们似乎不起作用:

Here is my code: 这是我的代码:

body #s4-workspace {
  left: 0;
  overflow-x: auto;
  overflow-y: scroll;
  position: relative;
}
<div class="" id="s4-workspace" style="height: 463px; width: 1280px;">other tags</div>

Here are the methods I've tried: 以下是我尝试过的方法:

1) 1)

$('body #s4-workspace').css('overflow-x','');
$('body #s4-workspace').css('overflow-y','');

2) 2)

$('body #s4-workspace').removeProp('overflow-x');
$('body #s4-workspace').removeProp('overflow-y');

If I output the overflow properties after either method, I see no change in their values. 如果我在任一方法之后输出overflow属性,我看到它们的值没有变化。
See my demonstration below: 请参阅下面的演示:

 $(document).ready(function() { try { $('body #s4-workspace').css('overflow-x', ''); $('body #s4-workspace').css('overflow-y', ''); $('body #s4-workspace').removeProp('overflow-x'); $('body #s4-workspace').removeProp('overflow-y'); alert($('body #s4-workspace').css('overflow-x')); } catch (e) { alert(e); } }); 
  body #s4-workspace { left: 0; overflow-x: auto; overflow-y: scroll; position: relative; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="s4-workspace" style="height: 463px; width: 1280px;">other tags</div> 

View on JSFiddle 在JSFiddle上查看

How can I remove overflow with jQuery? 如何用jQuery删除overflow

You just need to set them to their default values - visible : 你只需将它们设置为默认值- visible

$('body #s4-workspace').css('overflow-x','visible');
$('body #s4-workspace').css('overflow-y','visible');

Explanation 说明

According to jQuery's css() documentation : 根据jQuery的css()文档

Setting the value of a style property to an empty string ... does not ... remove a style that has been applied with a CSS rule in a stylesheet or element. 将样式属性的值设置为空字符串...不会...删除已在样式表或元素中应用CSS规则的样式。

I've demonstrated that issue below -- notice that there is no change in the overflow value: 我在下面演示了这个问题 - 请注意overflow值没有变化:

 $(function() { var $workspace = $('#s4-workspace'), $output = $('#outout'); $('div#output').html("<span>Old overflow value: " + $workspace.css('overflow-x') + "</span>"); $workspace.css('overflow-x', ''); $('div#output').html($('div#output').html() + "<span>Old overflow value: " + $workspace.css('overflow-x') + "</span>"); }); 
 #s4-workspace { overflow-x: auto; } #output span { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="s4-workspace">other tags</div> <div id="output"></div> 

However, an empty string will reset overflow to its default value if it's been set by jQuery : 但是, 如果由jQuery设置 ,则空字符串会将 overflow重置为其默认值:

 $(function() { var $workspace = $('#s4-workspace'), $output = $('#outout'); $workspace.css('overflow-x', 'auto'); $('div#output').html("<span>Old overflow value: " + $workspace.css('overflow-x') + "</span>"); $workspace.css('overflow-x', ''); $('div#output').html($('div#output').html() + "<span>Old overflow value: " + $workspace.css('overflow-x') + "</span>"); }); 
 #output span { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="s4-workspace">other tags</div> <div id="output"></div> 

Solution

The solution of setting the overflow value explicitly has been offered by ahoffner . ahoffner提供了 明确设置overflow值的解决方案。
Another idea is to apply overflow via a CSS class. 另一个想法是通过CSS类应用overflow Then you can remove it with jQuery's removeClass() rather than manipulating the style directly: 然后你可以用jQuery的removeClass()删除它,而不是直接操作样式:

 $(function () { var $workspace = $('#s4-workspace'), $output = $('#output'); $output.html("<span>Old overflow value: " + $workspace.css('overflow-x') + "</span>"); $workspace.removeClass('horizontal_scroll'); $output.html($output.html() + "<span>New overflow value: " + $workspace.css('overflow-x') + "</span>"); }); 
 #s4-workspace { left: 0; position: relative; } .horizontal_scroll { overflow-x:auto; } #output span { display:block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="s4-workspace" class="horizontal_scroll">other tags</div> <div id="output"></div> 

Incidentally: 偶然:

Warning: one notable exception is that, for IE 8 and below, removing a shorthand property such as border or background will remove that style entirely from the element, regardless of what is set in a stylesheet or element. 警告:一个值得注意的例外是,对于IE 8及更低版本,删除边框或背景等速记属性将完全从元素中删除该样式,无论样式表或元素中设置了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM