简体   繁体   English

动态jQuery工具提示

[英]Dynamic jQuery Tooltip

I have a pretty specific request. 我有一个非常具体的要求。 I have been looking through some other posts, but can't find a definitive answer on this, so help is appreciated. 我一直在浏览其他一些帖子,但是找不到确切的答案,因此我们非常感谢您的帮助。

I'm looking to get a jQuery tooltip that when hovered is a normal tooltip with the text centered. 我正在寻找一个jQuery工具提示,将其悬停时是一个以文本为中心的普通工具提示。 However, when you click the element, the tooltip widens to the left (the text would remain centered so it'd be appear to be moving left as the tooltips center moved left) and a dropdown menu slides out beneath the tooltip. 但是,当您单击该元素时,工具提示会向左加宽(文本将保持居中,因此当工具提示的中心向左移动时,该文本将显示为向左移动),并且下拉菜单将从工具提示下方滑出。 I am still on the fence on whether or not the widening is going to be necessary, but I want the ability to have the dropdown on click. 对于是否需要加宽,我仍然持保留态度,但是我希望能够单击下拉菜单。

Any and all help with this would be greatly appreciated. 任何和所有与此有关的帮助将不胜感激。 Thanks! 谢谢!

EDIT*** I have written some code that I can't really get to work. 编辑***我写了一些我无法真正开始工作的代码。 Right now I'm just trying to work it out with divs, hoping I can replace the "toolTip" div with a tooltip shape instead of just a box, but I want to get things working before I worry about that. 现在,我只是尝试使用div解决问题,希望我可以用工具提示形状而不是仅用盒子替换“ toolTip” div,但是在我担心这一点之前,我想让事情开始进行。 Any help on the following code is appreciated. 感谢以下代码的任何帮助。 Thanks! 谢谢!

I apologize in advance for the lack of some indentation and stuff, I don't know why my code doesn't ever copy/paste well into here. 由于缺少缩进和内容,我先向您致歉,我不知道为什么我的代码无法很好地复制/粘贴到此处。

HTML: HTML:

<div id="wrapper">

<div class="topIconNew">
</div>

<div class="topTip">
</div>

<div class="topDrop">
</div>

</div>

CSS: CSS:

div#wrapper {
margin:0 auto;
width:100%;
height:100%;
position:relative;
    top:0;
    left:0;
}  

.topIconNew {
background-color:red;
border:solid 1px #444444;
width:20px;
height:20px;
position:fixed;
    top:50px;
    left:450px;
cursor:pointer    
}

.topTip {
background-color:#d3d3d3;
border:solid 1px #444444;
width:80px;
height:20px;
position:fixed;
    top:70px;
    left:450px;    
}    

.topDrop {
background-color:#ffffff;
border:solid 1px #555555;
width:100px;
height:300px;
position:fixed;
    top:90px;
    left:450px;        
}    

jQuery jQuery的

$(document).ready(function() {

// tooltip hover
$("div.topIconNew").hover(
    function(){
        $("div.topTip").show();
    }
);        

//tooltip widening and dropdown menu

$("div.topIconNew").click(
    function(){
        //permanent tooltip
        $("div.topTip").show();
    },    
    function(){
        //widen tooltip
$("div.topTip").animate({width:200},"fast");
    },        
    function() {
        //show dropdown
        $("div.topDrop").slideDown(300);
    }
);
$("div.wrapper").click(
    function(){
        //hide dropdown (hide simultaneously)
        $("div.topDrop").hide();
    }
    function(){
        //hide tooltip (hide simultaneously)
        $("div.topTip").hide();
    {        
);

}); 

Any and all help is greatly appreciated. 任何帮助都将不胜感激。 Thanks! 谢谢!

Assuming from scratch I'd first build 从头开始假设我会先建立

  1. A dropdown menu class that can be absolutely positioned 可以绝对定位的下拉菜单类
  2. A tooltip class that can be absolutely positioned 可以绝对定位的工具提示类

It would be important for these classes to be self contained eg the dropdown should handle all bind events etc... First I'd hook up a hover element over whatever triggers the tooltip then position the tooltip and set the text. 这些类必须自包含,这很重要,例如,下拉列表应处理所有绑定事件等。首先,我将在一个触发工具提示的对象上悬停一个悬停元素,然后放置工具提示并设置文本。 I'd then attach an onClick event to the same item and inside that I'd: 然后,我将一个onClick事件附加到相同的项目和内部:

  1. Flag the tooltip as "hover out no longer destroys it" (probably use an external click on the document instead) 将工具提示标记为“悬停不再破坏它”(可能在文档上使用外部单击)
  2. Run an animate on the tooltip object that both sets the x position to x-200 (for instance) and also sets the width to with+200 ( .animate({x: '-=200', width: '+=200'}) ) 在工具提示对象上运行动画,既可以将x位置设置为x-200(例如),也可以将宽度设置为with + 200( .animate({x: '-=200', width: '+=200'})
  3. I'd attach an event listener on animate so at the end of the animation I then attach the dropdown relative to the tooltip 我将在动画上附加一个事件侦听器,因此在动画结束时,我将附加相对于工具提示的下拉列表

If you code them separately it should be easy to tie them all together. 如果将它们分别编码,则将它们捆绑在一起应该很容易。 You really just have to focus on the main evens in such a system: 您实际上只需要关注这样一个系统中的主要事件:

  1. Mouseover on item for tooltip 将鼠标悬停在工具提示的项目上
  2. Mouseout on item for tooltip 将鼠标悬停在工具提示项目上
  3. Click on item to fly out tooltip and flag for permanence 单击项目以弹出工具提示并标记永久性
  4. Attaching the dropdown 附加下拉菜单
  5. Responding to dropdown events 响应下拉事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM