简体   繁体   中英

Automatic scrolling in child element with max-height

I'm trying to create a console/terminal like element for my website.

终奌站

Basically, I want users to type in commands and give output. When the output is generated, it might be a couple of lines of info.

So when new output is displayed, I want to scroll down to the "new-output" element automatically.

<div class="container center">
<form class="about-form">
    <input type="text" class="about-input">
</form>
<div class="terminal">
    <div class="terminal-bar"><span class="title">info ><span class="terminal-height">terminal</span></span>
    </div>
    <p class="prompt">Name: Ruan Kranz</p>
    <p class="prompt">ROLE: full stack web developer</p>
    <p class="prompt">Level: 5+ years experience</p>
    <p class="prompt">Personality: Curious, passionate, analytical</p>
    <p class="prompt">Skills: Back-end, Front-end, DevOps, Scripting, Design</p>
    <p class="prompt">Interests: Coding, gaming, bitcoin, travel, reading, learning</p>
    <p class="prompt"></p>
    <p class="prompt">Type the words above ^1000</p>
    <p class="prompt">To leave type "exit" ^1000</p>
    <p class="prompt">Type "HELP" for a list of commands ^1000</p>
    <p class="prompt output new-output"></p>
</div>

I do this using velocity.js

$('.new-output').velocity(
    'scroll'
), {
    duration: 100
}

And this function works perfectly if the .terminal is not in a container with a max-height.

I have the following CSS for the .center container

 .center {
    margin: auto;
    max-width: 800px;
    min-height: 400px;
}

And the .terminal

 .terminal {
     position: relative;
     display: block;
     margin: auto;
     border-radius: 10px;
     box-shadow: rgba(0, 0, 0, 0.8) 0px 20px 70px;
     clear: both;
     max-height: 400px;
     overflow-x: hidden;
     overflow-y: visible;
 }

Why is it not working when I try to scroll down the user when using a container with a fixed height, and how can I achieve the desired outcome? I don't want the elements height (or size) to change, but I want to scroll the user down when new output is generated.

You could make use of javascript to achieve this.

<div class="container center">
<form class="about-form">
    <input type="text" class="about-input">
</form>
<div class="terminal">
    <div class="terminal-bar"><span class="title">info ><span class="terminal-height">terminal</span></span>
    </div>
    <p class="prompt">Name: Ruan Kranz</p>
    <p class="prompt">ROLE: full stack web developer</p>
    <p class="prompt">Level: 5+ years experience</p>
    <p class="prompt">Personality: Curious, passionate, analytical</p>
    <p class="prompt">Skills: Back-end, Front-end, DevOps, Scripting, Design</p>
    <p class="prompt">Interests: Coding, gaming, bitcoin, travel, reading, learning</p>
    <p class="prompt"></p>
    <p class="prompt">Type the words above ^1000</p>
    <p class="prompt">To leave type "exit" ^1000</p>
    <p class="prompt">Type "HELP" for a list of commands ^1000</p>
    <p id = "output-prompt" class="prompt output new-output"></p>
</div>

and use this in javascript.

window.location = window.location + '#output-prompt';

or

document.getElementById('output-prompt').scrollIntoView({
  behavior: 'smooth'
});

Hope this helps!!

you can add the following css class to the div/p tag where you want the scroll to get added in the output.

.scroll
{
float:left;
overflow-y: auto;
height: 460px;
}

I got it working from this questions solution: link

var $container = $('div'),
$scrollTo = $('#row_8');

$container.scrollTop(
    $scrollTo.offset().top - $container.offset().top + $container.scrollTop()
);

// Or you can animate the scrolling:
$container.animate({
    scrollTop: $scrollTo.offset().top - $container.offset().top + $container.scrollTop()
});​

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