我遇到問題。所以,我正在製作反應組件,並且我需要使用按鈕的工具提示。工具提示正在工作,但我無法將它放在我想要的位置(我的意思是位於按鈕的中心位置)。React組件不起作用
當我安慰日誌,它顯示了e.target.offsetLeft和e.target.offsetTop是0,但我給它從雙方保證金。
但實際上,當我把這個代碼,必須把提示,則不顯示整個工具提示:
tooltip.style.left = options.x + (options.w/2) - (tooltip.offsetWidth/2) + "px";
tooltip.style.top = (options.y - tooltip.offsetHeight - 10) + "px";
它是我整個代碼:
import React from 'react';
import ReactDOM from 'react-dom';
import Style from 'style-it';
var Ink = require('react-ink');
import FontIcon from '../FontIcon/FontIcon';
var IconButton = React.createClass({
getInitialState() {
return {
iconStyle: "",
style: "",
cursorPos: {},
};
},
render() {
var _props = this.props,
...
globalTooltip = null,
...
function createTooltip(options) {
var tooltip = document.createElement("div");
tooltip.className = "tooltip";
tooltip.appendChild(document.createTextNode(_props.tooltip));
document.body.appendChild(tooltip);
tooltip.style.left = options.x + (options.w/2) - (tooltip.offsetWidth/2) + "px";
tooltip.style.top = (options.y - tooltip.offsetHeight - 10) + "px";
globalTooltip = tooltip;
console.log(options);
};
function showTooltip(e){
var options = {
w: e.target.offsetWidth,
x: e.target.offsetLeft,
y: e.target.offsetTop,
};
createTooltip(options);
};
function removeTooltip(e){
globalTooltip.parentNode.removeChild(globalTooltip);
};
return(
...
);
}});
ReactDOM.render(
<IconButton ... tooltip="aaaaa" />, document.getElementById('app')
);
而在這一刻,我可以甚至沒有控制檯登錄選項對象:/
你可以使用'display'顯示/隱藏tooltip元素CSS屬性根據組件的道具設置爲'none'或'block'。它甚至會比更新DOM更快。 –
upvoted for @ free-soul的回答,最好用ReactJS的方式來完成。 –
它工作得很好,但我不能把它放在我想要的地方 – Karol