Asked  6 Months ago    Answers:  5   Viewed   20 times

Can I use JavaScript to check (irrespective of scrollbars) if an HTML element has overflowed its content? For example, a long div with small, fixed size, the overflow property set to visible, and no scrollbars on the element.

 Answers

27

Normally, you can compare the client[Height|Width] with scroll[Height|Width] in order to detect this... but the values will be the same when overflow is visible. So, a detection routine must account for this:

// Determines if the passed element is overflowing its bounds,
// either vertically or horizontally.
// Will temporarily modify the "overflow" style to detect this
// if necessary.
function checkOverflow(el)
{
   var curOverflow = el.style.overflow;

   if ( !curOverflow || curOverflow === "visible" )
      el.style.overflow = "hidden";

   var isOverflowing = el.clientWidth < el.scrollWidth 
      || el.clientHeight < el.scrollHeight;

   el.style.overflow = curOverflow;

   return isOverflowing;
}

Tested in FF3, FF40.0.2, IE6, Chrome 0.2.149.30.

Tuesday, June 1, 2021
 
Sabya
answered 6 Months ago
15

You didn't say anything about file types so I used HTML-5 formats that work in Safari.

<!DOCTYPE>
    <meta charset="UTF-8">
    <title> audio video</title>

    <audio id="audioInHTML" controls="controls">     
    <source src="audio.wav" type="audio/wav"/>
    </audio>
    </div>

    <video id="videoInHTML" width="320" height="240" controls="controls">
      <source src="movie.mp4" type="video/mp4" />
    </video>

    <div id="playButtonDiv" </div>

    <form>
    <input type="button" value="PlaySoundAndVideo" onClick="PlaySoundAndVideo('videoInHTML'),PlaySoundAndVideo('audioInHTML')">

    </div>

    <script>

    function PlaySoundAndVideo(soundObj,videoObj) {
    var soundAndVideo=document.getElementById(soundObj,videoObj);
    soundAndVideo.play();
    }


    </script>

    <style type="text/css">


    #playButtonDiv {
    position:absolute;
    top:800px;

    };   

    </style>
Sunday, August 15, 2021
 
aorfevre
answered 4 Months ago
38

Another solution, if you do not need to use the legend element, is to use an h1 and style appropriately. This works for me in both Chrome and FF.

<!DOCTYPE HTML>
<html>
<head>
    <title>a</title>
    <style>
        fieldset {
            height: 80px;
        }
            h1 {
                margin:0;
                margin-top:-1em;
                font-size:1em;
                background:white;
                width:33px;
            }   
        fieldset div {
            height: 100%;
            overflow-y: scroll;
        }

    </style>
</head>
<body>
    <fieldset>
        <h1>Test</h1>
        <div>
            Foo!<br/>
            Foo!<br/>
            Foo!<br/>
            Foo!<br/>
            Foo!<br/>
            Foo!<br/>
        </div>
    </fieldset>
</body>

Tuesday, August 24, 2021
 
ajay
answered 3 Months ago
34

Unfortunately, this is currently not possible at all. The input event is only fired once the value inside the input actually changes, which always comes after making your selection. The event does not hold information on how this was added.

An alternative would be to track the actual click or keypress events. However, neither the click event nor any of the keypress events seem to be fired when using the datalist (at least not in Chrome). Even if you bind them on the document or body, clicks or keypresses simply do not register when performed on the actual datalist.

As such, it is impossible to tell the difference between these methods of selecting a datalist option.

Sunday, August 29, 2021
 
Vinícius Gobbo A. de Oliveira
answered 3 Months ago
89

Hopefully, I've found a good blog post and download link for this Pure HTML5/JS/CSS3 project template. I've spent a lot of time today, searching a solution how to start a pure project without useless things which I never use in future. Finally, I found download link for purehtml (codepen official link is no longer exist) in article

Update 17-Mar-15:

Uploaded it to the dropbox, because previous link is stopped working

Monday, November 8, 2021
 
Koden
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share