Gen*_*ais 15 html javascript css jquery
是否有可能在鼠标悬停时立即显示锚标记的标题属性,而不仅仅是在秒之后.JavaScript/jQuery和CSS中的任何解决方案都很好.
title属性的处理取决于浏览器,并且没有定义任何API来控制它,更少在规范中指定.这包括延迟,显示的持续时间,使用的字体,字体大小等.
有迹象表明,可以使用其他技术替代的title属性.其他答案中提到了其中一些.请注意,简单的"CSS工具提示"可以简单灵活地在纯CSS中实现.使用这些技术时,要显示的数据通常不属于title属性,因为它的处理依赖于浏览器,因此存在显示特殊工具提示和浏览器实现的风险title.尽管可以防止后者,但是当启用脚本时,使用对任何内容都没有默认效果的属性(例如data-title=...或)会更简单data-tooltip=....
一种方法:
// textFrom : String, the attribute from which the text
// should come,
// delta : String or Number, the distance from the cursor at
// which the tooltip should appear
function instantTooltips(textFrom, delta) {
// if delta exists, and can be parsed to a number, we use it,
// otherwise we use the default of 5:
delta = parseFloat(delta) ? parseFloat(delta) : 5;
// function to handle repositioning of the created tooltip:
function reposition(e) {
// get the tooltip element:
var tooltip = this.nextSibling;
// setting the position according to the position of the
// pointer:
tooltip.style.top = (e.pageY + delta) + 'px';
tooltip.style.left = (e.pageX + delta) + 'px';
}
// get all elements that have an attribute from which we
// want to get the tooltip text from:
var toTitle = document.querySelectorAll('[' + textFrom + ']'),
//create a span element:
span = document.createElement('span'),
// find if we should use textContent or innerText (IE):
textProp = 'textContent' in document ? 'textContent' : 'innerText',
// caching variables for use in the upcoming forEach:
parent, spanClone;
// adding a class-name (for CSS styling):
span.classList.add('createdTooltip');
// iterating over each of the elements with a title attribute:
[].forEach.call(toTitle, function(elem) {
// reference to the element's parentNode:
parent = elem.parentNode;
// cloning the span, to avoid creating multiple elements:
spanClone = span.cloneNode();
// setting the text of the cloned span to the text
// of the attribute from which the text should be taken:
spanClone[textProp] = elem.getAttribute(textFrom);
// inserting the created/cloned span into the
// document, after the element:
parent.insertBefore(spanClone, elem.nextSibling);
// binding the reposition function to the mousemove
// event:
elem.addEventListener('mousemove', reposition);
// we're setting textFrom attribute to an empty string
// so that the CSS will still apply, but which
// shouldl still not be shown by the browser:
elem.setAttribute(textFrom, '');
});
}
// calling the function:
instantTooltips('title', 15);Run Code Online (Sandbox Code Playgroud)
[title] {
display: block;
margin: 0 0 1em 0;
}
/*
hiding, and styling, the elements we'll be creating
*/
[title] + span.createdTooltip {
display: none;
border: 2px solid #f90;
background-color: rgba(255, 255, 255, 0.8);
padding: 0.2em 0.5em;
border-radius: 0.75em;
}
/*
showing the created elements on hovering the element we want to
show tooltips for
*/
[title]:hover + span.createdTooltip {
display: block;
position: absolute;
}Run Code Online (Sandbox Code Playgroud)
<span title="This is the span's title">A span element</span>
<img src="http://placekitten.com/g/250/250" title="A kitten." />
<input title="This is an input element's title." value="This input has a title" />Run Code Online (Sandbox Code Playgroud)
参考文献:
document.createElement().document.querySelectorAll().Element.classList.in运营商.Node.cloneNode().Node.nextSibling.Node.parentNode.parseFloat().typeof.Bootstraps ToolTip 插件在这方面做得非常好,并且响应速度更快/更快。
只需要默认的引导文件即可运行。
CSS 可以更改以满足您的要求。
更多信息和示例可以在此处显示:
http://getbootstrap.com/javascript/#tooltips
| 归档时间: |
|
| 查看次数: |
24166 次 |
| 最近记录: |