繁体   English   中英

以最大高度自动滚动子元素

[英]Automatic scrolling in child element with max-height

我正在尝试为我的网站创建一个控制台/终端之类的元素。

终奌站

基本上,我希望用户键入命令并提供输出。 生成输出时,可能需要几行信息。

因此,当显示新输出时,我想自动向下滚动到“ new-output”元素。

<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>

我使用Velocity.js做到这一点

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

如果.terminal不在最大高度的容器中,则此功能可以完美工作。

我有以下CSS用于.center容器

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

.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;
 }

在使用固定高度的容器尝试向下滚动用户时,为什么它不起作用?如何获得所需的结果? 我不希望元素的高度(或大小)发生变化,但是我想在生成新输出时向下滚动用户。

您可以利用javascript实现此目的。

<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>

并在javascript中使用它。

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

要么

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

希望这可以帮助!!

您可以将以下css类添加到div / p标签中,您希望在其中将滚动添加到输出中。

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

我从这个问题解决方案中获得了工作: 链接

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()
});​

暂无
暂无

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

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