简体   繁体   English

一个功能来规则多个按钮,然后一些

[英]One function to rule multiple buttons, and then some

I have 7 buttons. 我有7个按钮。 They are distributed on top of a background image and interacting with it. 它们分布在背景图像之上并与之交互。 They are placed absolutely. 他们绝对放置。 I have created a jQuery function to animate one of the buttons height. 我创建了一个jQuery函数来设置其中一个按钮高度的动画。 The button expands upwards. 按钮向上扩展。 Check it out here: http://hdpano.no/bf/newindex.html and click the top left button named Deck 8. 在这里查看: http//hdpano.no/bf/newindex.html并单击名为Deck 8的左上角按钮。

I wish this function to handle all the buttons, but there are some variables. 我希望这个函数能够处理所有按钮,但是有一些变量。 The baseline of each button varies, and i need to subtract from it as i expand the height. 每个按钮的基线都有所不同,我需要在扩展高度时从中减去。 I also wish to close any other open button if one clicks another. 如果再点击另一个按钮,我也希望关闭任何其他打开按钮。

Here is the jQuery code: 这是jQuery代码:

jQuery(document).ready(function() {

$('#link8').toggle(
function()
{
  $('#deck8').animate({height: "25px",top:"202"}, 500);
},
function()
{
$('#deck8').animate({height: "150",top:"76"}, 500);

});

});

The function is quite basic and I have stripped it of all my attempts to make it work with the other buttons. 该功能是非常基本的,我已经剥夺了我使用其他按钮的所有尝试。

This does what you're looking for. 这就是你要找的东西。 Replace the code in your page with this... 用这个替换页面中的代码......

<script type="text/javascript">
    jQuery(document).ready(function() {
        $('.link').click(function() {
            var $me = $(this);
            if ($me.height() == 150) {
                $me.animate({height: "25px",top:"+=126"}, 500);
            } else {
                $(".link").each(function() {
                    if ($(this) != $me) {
                        if ($(this).height() == 150) {
                            $(this).animate({height: "25px",top:"+=126"}, 500);
                        }
                    } 
                });
                $me.animate({height: "150px",top:"-=126"}, 500);
            }
        });
    });
</script>

You can toggle the position with += and -= so it uses relative positioning, rather than absolute positioning, so that code affects all the divs on the page with class "link". 您可以使用+ =和 - =切换位置,因此它使用相对定位而不是绝对定位,以便代码影响页面上具有类“链接”的所有div。

this in the toggle functions would be the element that is clicked. 切换函数中的this将是被单击的元素。

Here is what I would do: 这是我要做的:

  • remove the <br/> tags. 删除<br/>标签。 Use margin/padding to achieve spacing. 使用边距/填充来实现间距。

  • basically you want to expand/collapse the element ".link" (the container) for the height of the contained <ul> . 基本上你想扩展/折叠元素“.link”(容器)为包含的<ul>的高度。

  • use "+=" or "-=" with the animate function to automatically add/remove the specified value. 使用“+ =”或“ - =”与animate函数一起自动添加/删除指定的值。

  • as your buttons start collapsed , you should invert the two functions in the toggle 当您的按钮开始折叠时 ,您应该反转切换中的两个功能

Here a code that is more general: 这里的代码更通用:

jQuery(document).ready(function() {

    // on click of any link with class ".linkContent"
    $('.linkContent').toggle(
    function() {
            // get the parent ".link" container
        var $parent = $(this).parent(),
            // get the full height of the <ul> to show/hide + the height of the link
            h = $parent.find('ul').outerHeight() + $(this).outerHeight();

        // animate using += and -= to avoid setting explicit values
        $parent.animate({ height: '+=' + h, top: '-=' + h }, 500);
    },
    function() {
        var $parent = $(this).parent(),
            h = $parent.find('ul').outerHeight() + $(this).outerHeight();
        $parent.animate({ height: '-=' + h, top: '+=' + h }, 500);
    });

});

The following demo shows the code in action. 以下演示显示了正在运行的代码。 You might have to tweak it a bit to get the exact height to add/remove but you get the idea: 您可能需要稍微调整一下以获得添加/删除的确切高度,但您会明白:

DEMO DEMO

What you want to do is add a class eg .deck to each button, then toggle .deck'. Inside the toggle function use 你想要做的是为每个按钮添加一个类,例如.deck ,然后切换.deck'. Inside the toggle function use .deck'. Inside the toggle function use $(this)` to refer to the current button. .deck'. Inside the toggle function use $(this)`来指代当前按钮。

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

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