javascript - 无论内容长度如何,都保持错误工具提示始终垂直对齐

标签 javascript html css prototypejs

我正在创建一个错误工具提示,如果用户将鼠标悬停在输入字段上,它将可见。这个工具提示现在差不多完成了,我只需要让它相对于输入字段垂直对齐。我尝试了很多方法,但都没有成功。

Fiddle

input {
    height: 30px;
    width: 278px;
}
/* Error Validations css */
.error-field {
    position: relative;
    overflow: visible;
    display: inline-block;
}
.error-field > input:focus {
    background-color: white;
    outline: none;
}
.error-field > input {
    background-color: #ffdedf;
    border: 1px solid red !important;
    border-radius: 3px;
}
.error-field:hover {
    /* IE Fix */
}
.error-field:hover:after {
    content: attr(data-error);
    display: inline-block;
    vertical-align: middle;
    padding: 3px;
    position: absolute;
    top: 50%;
    margin-top: -10px;
    z-index: 1;
    display: block;
    background-color: #ffdd67;
    left:100%;
    width: 200px;
    line-height: 15px;
    border: 1px solid #b07317;
    margin-left: 15px;
    border-radius: 3px;
    -ms-border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
}

我正在尝试避免使用 javascript(或原型(prototype)),但如果仅使用 css 是不可能的,那么我很乐意使用它。

我支持的浏览器有IE9、chrome和firefox。

我们将不胜感激。

最佳答案

使用负数 translateY 而不是负数 margin-top (supported also on IE9)
http://jsfiddle.net/x9uW5/1/

.error-field:hover:after {
    ...
    position: absolute;
    top: 50%;
    -webkit-transform: translate(0, -50%);
    -moz-transform: translate(0, -50%);
    -ms-transform: translate(0, -50%);
    transform: translate(0, -50%);
}

这将始终确保居中对齐,无论工具提示中包含多少文本

关于javascript - 无论内容长度如何,都保持错误工具提示始终垂直对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21306506/

相关文章:

javascript - AngularJs,如何分配动态模型名称

javascript - typescript 符号异步函数作为类型/接口(interface)属性

Javascript 即使有额外的 ../也能找到外部文件

html - 在不增加宽度的情况下增加文本的高度

javascript - 粘性导航 - 使用 stickUp.js 插件

javascript - SetInterval 在 Firefox 中看不到函数

html - Materialise 预加载器

css - 为什么我的 Divs 没有跨越 100% 的宽度?

html - 如何防止 Mozilla 在 HiDPI 显示器上放大图像?

css - jQueryUI.对话框 : override single css style property?