Sliding Divs - >多

时间:2012-06-22 07:31:02

标签: jquery css-transitions

我用这个脚本用jquery和css动画一些div。现在我想使用超过这两个按钮。相反,下面应该有几个列表。但我不能只复制每个jquery脚本并更改变量的名称。

以下是示例:http://jonathanstark.com/labs/simple-sliding-example-using-css3-animations.html

我如何更改变量和脚本,因此脚本将适用于任何“settings”+“home”div的每个组合?像垂直菜单一样,在每个“home”div上,组合的“settings”div会滑入。

<script>
            function goTo(targetPanelId) {
                var home = document.getElementById('home');
                var settings = document.getElementById('settings');
                if (targetPanelId === 'settings') {
                    home.className = 'slide left out';
                    settings.className = 'slide left in';                    
                } else {
                    home.className = 'slide right in';
                    settings.className = 'slide right out';                    
                }
                return false;
            }
        </script>

1 个答案:

答案 0 :(得分:0)

这是我的代码,希望这是你想要的:

jsfiddle

<强> HTML

<div id="container">
    <div id="home">
        <h1>Home</h1>
        <a href="#" class="menu" data-hash="settings-1"><span>Go to Settings 1</span></a>
        <a href="#" class="menu" data-hash="settings-2"><span>Go to Settings 2</span></a>
        <a href="#" class="menu" data-hash="settings-3"><span>Go to Settings 3</span></a>
        <p>Animation Example</p>
        <p>When a button is clicked/tapped, the JavaScript merely assigns class names to the panels. The CSS then jumps into action based on the new class names.</p>
    </div>
    <div id="settings">
        <h1>Settings</h1>
        <a id="settings-1" href="#" class="menu" data-hash="home"><span>Go to Home 1</span></a>
        <a id="settings-2" href="#" class="menu" data-hash="home"><span>Go to Home 2</span></a>
        <a id="settings-3" href="#" class="menu" data-hash="home"><span>Go to Home 3</span></a>
    </div>
</div>

<强> JS

function goTo(link) {
    var hash = link.dataset['hash'],
        target = document.getElementById(hash),
        source = link.parentNode,
        isHome = source.id == 'home';

    if(isHome) {
        target.className = 'slide left in';
    }else{
        link.className = 'slide right out';
    }

    return false;
}

var navs = document.getElementsByClassName('menu');
for(var i in navs) {
    var obj = navs[i];
    if(typeof obj == 'object') {
        obj.addEventListener('click', function() {
            goTo(this);
        });
    }
}

注意删除的p标签,我必须使用其按钮样式的跨度使其父节点的整个宽度成为锚点,因为否则100%x-translation将不是完整的动画。