简体   繁体   中英

How can I prevent horizontal scrollbars from showing on my floating div?

I have created a left-side menu that expands on mouse enter. This works, but I have one minor issue: Is there a way / How can I stop the horizontal scrollbar from showing at the bottom of the left panel?

在此处输入图片说明

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>
<title>Test</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<style>
  #leftpanel {
    left: 0;
    width: 100px;
    overflow: auto;
    height: 100%;
    background: gray;
    border: 2px solid black;
    position: fixed;
    z-index:100;
  }
  #centerPanel {
    top: 0px;
    position: relative;
    left: 100px;
    height: 600px;
    width:calc(100% - 100px);
    background: yellow
  }
</style>

<script>
$(function() {
//  alert("ready");
  $("#leftpanel").mouseenter(function() {
    var rect = document.getElementById("tbl").getBoundingClientRect();
    var wid = (rect.left + rect.width);
    var css = {};
    css.width = wid;
    $(this).animate(css, "slow");
  });
  $("#leftpanel").mouseleave(function() {
    $(this).animate({width: "100px"}, "slow");
  });
});
</script>

</head>
<body>
<div id="leftpanel"><table id="tbl"><tr><td style="white-space: nowrap">This is some longerish text using this as an example</td></tr></table></div>
<div id="centerPanel"><a href="">Foo</a></div>
</body>
</html>

In #leftpanel, make overflow as hidden instead of auto

#leftpanel {
  left: 0;
  width: 100px;
  overflow: hidden;
  height: 100%;
  background: gray;
  border: 2px solid black;
  position: fixed;
  z-index:100;
}

Visit https://jsfiddle.net/w7652rzx/

Short answer:

change

overflow: auto;

to

overflow: hidden;

From the Docs :

auto :
[...] Desktop browsers provide scrollbars if content overflows.

hidden :
Content is clipped if necessary to fit the padding box. No scrollbars are provided.

In the left panel style need to use overflow:hidden. Before you have used auto.

This means, your text was long so it's automatically added scroll in the bottom. Use below code,

#leftpanel {
    left: 0;
    width: 100px;
    overflow: hidden;
    height: 100%;
    background: gray;
    border: 2px solid black;
    position: fixed;
    z-index:100;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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