=>實現功能:採用JS事件驅動以及CSS內聯樣式類比工具提示;
<html>
<head>
<title>使用CSS的工具提示</title>
<script type="text/javascript">
function Tooltip() {
this.tooltip = document.createElement("div");
this.tooltip.style.position = "absolute";
this.tooltip.style.width = "250px";
this.tooltip.style.height = "150px";
this.tooltip.style.backgroundColor = "lightgray";
this.tooltip.style.border = "3px outset black";
this.tooltip.style.visibility = "visible";
this.tooltip.id = "tooltip";
this.tooltip.className = "tooltipShadow";
this.content = document.createElement("div");
this.content.style.position = "relative";
this.content.id = "tipContent";
this.content.className = "tooltipContent";
this.tooltip.appendChild(this.content);
console.log("Tooltip類");
}
Tooltip.prototype.show = function(text, x, y) {
this.content.innerHTML = text;
this.tooltip.style.left = x + "px";
this.tooltip.style.top = y + "px";
this.tooltip.style.visibility = "visible";
if(this.tooltip.parentNode != document.body) {
document.body.appendChild(this.tooltip);
}
console.log("Tooltip show方法");
}
Tooltip.prototype.hide = function() {
this.tooltip.style.visibility = "hidden";
console.log("Tooltip hide方法");
}
function fireFn() {
// 擷取使用者選擇的文本
var text = "";
if(window.getSelection) {
text = window.getSelection().toString();
}else if(document.getSelection) {
text = document.getSelection();
}else if(document.selection) {
text = document.selection.createRange().text;
}
var tip = new Tooltip();
tip.show(text, 100, 100);
setTimeout(function() {
tip.hide();
}, 2000);
}
</script>
</head>
<body>
<p onmouseup="fireFn()">好多即將做軟體的或做的時間不長的同仁大多時候關注的是新技術,是創造性;但就和陽光下總有陰影一樣,
不管方法如何更迭,總有些東西無法徹底改善。</p>
</body>
</html>