【问题标题】:Input's caret height输入的脱字符高度
【发布时间】:2017-06-26 06:50:18
【问题描述】:

我需要帮助来更改插入符号的高度 - 无需更改视觉效果(只要它们有效,就允许使用黑客/技巧)。
我的意思是我不想更改字体的大小(高度和宽度) ,边距,填充,颜色,背景,边框等,如果我确实更改它以实现插入符号的样式,则使其成为可能,以便我可以像普通输入一样将其放回原处,并具有样式功能。

我最好的尝试是以下代码:

body{
  background-color: lightBlue;
}
#background{
  color: red;
  height: 61px;
  margin-top: 0px;
  text-shadow: 0 0 0 transparent;
  width: 173px;
  -webkit-text-fill-color: transparent;
  -webkit-transform: scale(1,calc(1/3));
}
#text{
  background-color: transparent;
  border-color: transparent;
  color: red;
  left: 8px;
  pointer-events: none;
  position: absolute;
  top: 103px;
}
<span>Caret's height (pixels):</span>
<br>
<br>
<input placeholder="15 (default)" spellcheck="false">
<br>
<br>
<input id="background" oninput="document.getElementById('text').value=this.value" spellcheck="false">
<input id="text" placeholder="5 (1/3)" onfocus="this.blur()" spellcheck="false" tabindex="-1">

如果需要,请忽略文本的选择怪异。我暂时不关心这个。

如果您有更好的解决方案,请告诉我。这只是我一直在做的一些测试,我并不完全需要颜色为红色,其他属性为它们的样子,但我确实希望它仍然可以将它们更改为我想要的任何东西。

如果人们不一样(使用 Chrome),我会如下所示:

【问题讨论】:

  • 快速浏览一下,我想说输入元素是不可能的。您可以使用内容可编辑的 div 来模拟输入。
  • 这在技术上是不可能的@Teemu,这就是我说黑客/技巧的原因。解决方法是正确的词。
  • 致编辑人员:请不要将代码转入 sn-p,因为这通常会破坏结果的视觉效果。为确保您获得与我相同的结果,请在您的网页中进行测试。
  • @user7393973 sn-p 对我来说很好用 c2n.me/3HiIm9u
  • 只是出于好奇 - 你为什么要这样做?我认为没有合理的理由证明让这项工作跨平台的努力是合理的。最重要的是,它打破了用户体验约定。这并不意味着你没有正当理由——我只是好奇它可能是什么;)

标签: javascript html css google-chrome


【解决方案1】:

这可以通过“黑客”来完成。

您需要结合 javascript、css 和 span 来代替输入框...

source: codepen

source: blog

source: jsfiddle

document.documentElement.setAttribute("class", "js");

var searchFauxInput = document.querySelector(".fb-Search_FauxInput");
var searchBox = document.getElementById("Input");

searchBox.addEventListener("keyup", function copyInput(event) {
      searchFauxInput.textContent = searchBox.value;
      searchBox.setAttribute("value", searchBox.value);
}, false);
* {
box-sizing: border-box;
}

body {
  background-color: #555;
  font-family: sans-serif;
}

.fb-Search {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  height: 44px;
  padding: 5px 70px 5px 5px;
  width: 400px;
  position: relative;
  background-color: #e4e4e4;
}

.fb-Search_Input {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}

.js .fb-Search_Input {
  position: absolute;
  left: -100vw;
}

.fb-Search_FauxInput {
  display: none;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  max-width: 80%;
  margin-top: 14px;
  border: 0;
  font-size: 20px;
  font-size: 1.3rem;
  color: #777;
  background-color: #e4e4e4;
  border-right: 3px solid transparent;
  height: 3px;
}

.js .fb-Search_FauxInput {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
}

.fb-Search_Input:focus ~ .fb-Search_FauxInput {
  -webkit-animation: pulseAttention 1.5s cubic-bezier(.215, .61, .355, 1) forwards infinite;
  animation: pulseAttention 1.5s cubic-bezier(.215, .61, .355, 1) forwards infinite;
}

.fb-Search_Label {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  padding: 5px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  color: #a7a7a7;
  font-size: 15px;
}

.fb-Search_Input:not([value=""]) ~ .fb-Search_Label {
  -webkit-box-pack: end;
  -ms-flex-pack: end;
  justify-content: flex-end;
}

@-webkit-keyframes pulseAttention {
  50% {
    border-color: green;
  }
}

@keyframes pulseAttention {
  50% {
    border-color: green;
  }
}
<div class="fb-Search">
	<input id="Input" class="fb-Search_Input" value="">
	<span class="fb-Search_FauxInput" dir="rtl"></span>
	<label class="fb-Search_Label" for="Input">Search</label>
</div>

【讨论】:

    猜你喜欢
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 2017-11-13
    • 2012-04-08
    • 2022-01-08
    相关资源
    最近更新 更多