gpt4 book ai didi

javascript - 放置按钮,使其不会随内容滚动

转载 作者:行者123 更新时间:2023-12-02 21:24:25 26 4
gpt4 key购买 nike

我有这个代码:

pre[class*="language-"] {
top: 0;
position: relative;
border-radius: 3px;
}

#copybutton {
position: absolute;
right: 1em;
top: 1em;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
border: 0px transparent;
font-size: 0.725rem;
padding: 3px;
border-radius: 5px;
background-color: #ebebeb;
transition: background-color 0.25s ease-in-out;
}
<link href="https://prismjs.com/themes/prism.css" rel="stylesheet">
<script src="https://prismjs.com/prism.js"></script>
<pre class="language-js">
<button id="copybutton">Copy</button>
<code>const name = "" //a really really really really really really really really really really really really really really really really long </code>
</pre>

我想定位按钮,以便当用户水平滚动代码时,按钮不应移动到其位置。但是当用户滚动页面(而不是代码)时,它应该移动。 CSS 和 JS 答案值得赞赏。请不要用 jQuery 回答。

最佳答案

添加包装器 div ,使 that 相对,而不是 pre本身,并将按钮从 pre 中取出.

.code-display-wrapper {
position: relative;
}

pre[class*="language-"] {
border-radius: 3px;
}

#copybutton {
position: absolute;
right: 1em;
top: 1em;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
border: 0px transparent;
font-size: 0.725rem;
padding: 3px;
border-radius: 5px;
background-color: #ebebeb;
transition: background-color 0.25s ease-in-out;
}
<link href="https://prismjs.com/themes/prism.css" rel="stylesheet">
<script src="https://prismjs.com/prism.js"></script>

foo<br>foo<br>foo<br>foo<br>

<div class="code-display-wrapper">
<button id="copybutton">Copy</button>
<pre class="language-js">
<code>const name = "" //a really really really really really really really really really really really really really really really really long </code>
</pre>
</div>

foo<br>foo<br>foo<br>foo<br>foo<br>foo<br>foo<br>foo<br>foo<br>foo<br>foo<br>foo<br>foo<br>foo<br>foo<br>

关于javascript - 放置按钮,使其不会随内容滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60771472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com