简体   繁体   English

尽管width:0,Textarea滚动条不会消失;

[英]Textarea scrollbar doesn't disappear despite width:0;

No scrollbar in Chrome, but in Firefox and IE, it doesn't disappear although the element width is 0: Chrome中没有滚动条,但是在Firefox和IE中,即使元素宽度为0,它也不会消失:

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Textarea Scrollbar</title>
    <style>
        textarea {
            height: 200px;
            width: 0;
            margin: 0;
            border: 0;
            padding: 0;
        }
    </style>
</head>

<body>
    <textarea id="textarea">
        Hello, world!
        ...
    </textarea>
    <input type="text" value="0" id="input" oninput="resize();">
    <script>
        function resize() {
            document.getElementById('textarea').style.width = document.getElementById('input').value + 'px';
        }
    </script>
</body>

</html>


DEMO DEMO

What's the reason? 什么原因? What's a cross-browser solution so the textarea behaves like in Chrome? 什么是跨浏览器解决方案,因此文本区域的行为类似于Chrome?

Try overflow:hidden; 尝试overflow:hidden; in the css for the text area attributed ID 在CSS中为文本区域指定的ID

EDIT 编辑

DEMO DEMO

Javascript 使用Javascript

//this function attaches eventListener to the element and it is compatible with legacy browsers as well
function attach(element,listener,ev,tf){

    if(element.attachEvent) {

        //support for older IE (IE7 inclusive)
        element.attachEvent("on"+listener,ev);

        }else{

        //modern browsers
        element.addEventListener(listener,ev,tf);

        }

    }

var newInterval; //we use this to set interval and check say every 200 milliseconds
                 //whether the *height* of our *textarea* has changed and if it has
                 //than we set its *overflow* to *auto*, so the *scrollbar* will be
                 //visible, but when the *height* is *less or equal to 200* we set
                 //its *overflow* to *hidden*, so the *scrollbar* isn't visible! 

var textarea = document.getElementById('textarea');

var input = document.getElementById('input');

//here we check if the value of our input element has changed than we change the width of our textarea 
attach(input,'input',function(){

textarea.style.width = input.value + 'px';

},false);


//when our textarea recieves focus (is clicked on) we start running the interval and
//check every 200 milliseconds if the height of the textarea is equal or greater than
//200px we set its overflow to auto so the scrollbar becomes visible, and when the
//height is equal or less than 200px we set our textareas overflow to hidden, so no
//scrollbar is visible

attach(textarea,'focus',function(){

 newInterval = setInterval(function(){

    height = textarea.scrollHeight;

        if(height>=200){

            textarea.style.overflow = 'auto'

        }else textarea.style.overflow = 'hidden';

    },200);

},false);

//here we clear our interval, as our textarea has lost focus
// and there is no need to further run our interval  
attach(textarea,'blur',function(){ clearInterval(newInterval); },false);

HTML HTML

  <textarea id="textarea">
        Hello, world!
        ...
  </textarea>
  <input type="text" value="0" id="input" oninput="resize();">

CSS CSS

textarea{

    height: 200px;
    width: 0;
    margin: 0;
    border: 0;
    padding: 0;
    overflow:hidden;
    border:1px solid red;

}

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

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