1

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

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

如何更改变量和脚本,以便脚本适用于任何“设置”+“主页”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>
4

1 回答 1

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 平移将不是完整的动画。

于 2012-06-22T10:45:37.803 回答