简体   繁体   English

如何让DIV用jquery做到这一点?

[英]How can I make DIVs do this with jquery?

I am trying to reproduce the lock function on posts on facebook with jquery and php/mysql. 我正在尝试使用jquery和php / mysql在Facebook上的帖子上重现锁定功能。

Below is an image showing the different actions it does when clicked on. 下面的图像显示了单击后它会执行的不同操作。

I think you would do somehing like on hover, show a hidden div with a tooltip, on hover off remove it. 我想您会做类似悬停的操作,显示带有工具提示的隐藏div,悬停时将其删除。 On click event show another hidden div but somehow has to change the button's colors as well. 在单击事件中,显示另一个隐藏的div,但也必须以某种方式更改按钮的颜色。 When the menu div is clicked open it has a menu and if any of these are clicked it needs to send the result to backend script with ajax. 单击菜单div时,将打开一个菜单,如果单击了菜单中的任何一个,则需要将结果发送到带有ajax的后端脚本。 After clicking an option or clicking outside of the divs it will hide all the divs, maybe it is just a click anywhere closes it so maybe a toggle can be used? 单击某个选项或在div外部单击后,它将隐藏所有div,也许只是单击即可将其关闭,因此可以使用切换开关吗?

Can anyone clarify I am going in the right direction. 任何人都可以澄清我朝着正确的方向前进。 I havent used jquery very much or javascript. 我还没有使用过jQuery或JavaScript。 Any examples of something like this or help would be greatly appreciated. 诸如此类或帮助的任何示例将不胜感激。

fb http://img2.pict.com/ed/9a/3a/2341412/0/screenshot2b166.png fb http://img2.pict.com/ed/9a/3a/2341412/0/screenshot2b166.png

You don't need JavaScript for the hover. 您不需要将JavaScript悬停。 Make an element that serves as your tooltip and position it above your dropdown button. 制作一个用作工具提示的元素,并将其放置在下拉按钮上方。 Then make a parent <div> for both. 然后为两者都<div><div> Your HTML should look something like this: 您的HTML应该如下所示:

<div id="container">
    <div id="button">...</div>
    <div id="tooltip">...</div>
</div>

Once you've done that, you can use CSS to position the tooltip and show it when necessary. 完成此操作后,您可以使用CSS定位工具提示,并在必要时显示它。

#container {
    /* All child elements should be positioned relative to this one. */
    position: relative;
}

#container #tooltip {
    /* Hide by default. */
    display: none;
    /* Place the tooltip 2px above the button. */
    position: absolute;
    bottom: 2px;
    right: 0px;
}

#container #button:hover + #tooltip {
    /* Show it when someone's hovering over the button. */
    display: block;
}

To show the drop-down box, you probably will need JavaScript. 要显示下拉框,您可能需要JavaScript。 Add another element to the container: 向容器添加另一个元素:

<div id="container">
    <div id="button">...</div>
    <div id="tooltip">...</div>
    <ul id="selection">
        <li>Something</li>
        <li>Something Else</li>
        <li>A Third Thing</li>
    </ul>
</div>

Position it as you like using position: absolute and hide it using display: none . 使用position: absolute随意定位它,并使用display: none隐藏它。 Then show it when we click on the button: 然后在我们单击按钮时显示它:

$('#button').click(function() {
    $('#selection').show();
});

You can then make your sub-items do whatever they like, as long as they also hide #selection . 然后,只要子项目也隐藏#selection ,您就可以使其子项目做任何他们想做的事情。

$('#selection li').click(function() {
    // do something
    $('#selection').hide();
});

Finally, you want to change the background and text colours upon hover. 最后,您想在悬停时更改背景和文本颜色。 That's easy enough: 这很容易:

#selection li {
    background-color: #ccc;
    color: black;
}

#selection li:hover {
    background-color: black;
    color: white;
}

This won't work perfectly in IE 6 or (I believe) 7 - you'll need to investigate alternative solutions for that. 这在IE 6或(我相信)7中无法完美运行-您需要研究替代解决方案。 Either use JavaScript or check out IE7.js and IE8.js . 使用JavaScript或签出IE7.js和IE8.js。

Here is the approach I would take: 这是我要采取的方法:

  1. For hovering check out jQuery's hover event to change the different image states 对于悬停,请检查jQuery的悬停事件以更改不同的图像状态
  2. For the tooltip there are several jQuery plugins such as qTip that you can achieve something like this 对于工具提示,有几个jQuery插件(例如qTip) ,您可以实现类似这样的功能
  3. For clicking, jQuery's click event will do the trick 对于单击,jQuery的click事件将解决问题
  4. The dropdown will be a little trickier. 下拉菜单会有些棘手。 You will need to use a combination of ajax methods and selector methods to change the page (ie the bullet) 您将需要结合使用ajax方法选择器方法来更改页面(即项目符号)
  5. Finally you will have to do a request of some sort when the page initially loads to find out which setting is selected, then display the selection. 最后,当页面最初加载时,您将必须进行某种请求,以找出选择了哪个设置,然后显示选择。 This could be done either with php as the page loads, or an ajax request as mentioned above. 这可以通过页面加载时的php或如上所述的ajax请求来完成。

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

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