【问题标题】:How can I change the hashtag names in anythingslider?如何更改任何东西滑块中的主题标签名称?
【发布时间】:2012-01-16 23:59:08
【问题描述】:

我正在使用anythingslider,如果您熟悉它,您可能会知道它具有以下功能:

hashTags : true,      // Should links change the hashtag in the URL?

您可以使用它为每张幻灯片生成唯一的 URL,就好像它们是页面一样。但缺点是它会为主题标签生成通用名称。 (例如

http://www.mysite.com/#&panel1-2

我的问题是,如何将这些主题标签的名称更改为每张幻灯片的自定义名称?

现在我正在使用以下功能使其在导航栏中显示每个幻灯片标题:

navigationFormatter : function(i, panel){
    return panel.find('h6').text();
}

我所做的只是在<h6> 元素中添加标题。那么,如何使用这些标题来重命名主题标签?

【问题讨论】:

    标签: javascript jquery jquery-plugins slider anythingslider


    【解决方案1】:

    您可以关闭默认哈希并使用onSlideComplete 回调来更新窗口哈希(demo; 和full screen demo):

    *注意:h6 也有一个 id(不允许空格)与 h6 的文本(允许空格,并在导航选项卡中使用)

    HTML

    <ul id="slider">   
        <li>
            <img src="http://placekitten.com/320/200" alt="" />
            <h6 id="meow">Meow</h6>
        </li>
        <li>
            <img src="http://placehold.it/320x200" alt="" />
            <h6 id="grey">Grey</h6>
        </li>
        <li>
            <img src="http://placebear.com/320/200" alt="" />
            <h6 id="rawr">Rawr</h6>
        </li>
        <li>
            <img src="http://dummyimage.com/320x200/000/fff.jpg" alt="" />
            <h6 id="bnw">Black & White</h6>
        </li>
        <li>
            <img src="http://placedog.com/320/200" alt="" />
            <h6 id="woof">Woof</h6>
        </li> 
    </ul>
    

    脚本

    $('#slider').anythingSlider({
    
        // Should links change the hashtag in the URL?
        hashTags: false,
    
        // Details at the top of the file on this use (advanced use)
        navigationFormatter: function(index, panel) {
            return panel.find('h6').text();
        },
    
        // Callback when the plugin finished initializing
        onInitialized: function(e, slider) {
            slider.$items.find('h6').each(function(){
                // add some prefix to the id so setting the hash doesn't
                // make the page jump
                this.id = "tmp_" + this.id;
            });
        },
    
        // Callback when slide completes - no event variable!
        onSlideComplete: function(slider) {
            var hash = '#' + slider.$currentPage.find('h6')[0].id.replace(/tmp_/,'');
            // remove prefix so hash works on page load
            window.location.hash = hash;
        }
    
    });
    

    这里的another demo 使用面板 ID 而不是 h6 ID。

    【讨论】:

    • 哇,太棒了!非常感谢。唯一的缺点是它向下滚动页面以聚焦正在处理的#id``。 (您可以在我的 WIP 网站中看到:anekdamian.com/Insurance)。
    • 哦,是的,我忘了...我更新了演示,为 ID 添加了前缀,这样页面就不会跳转了。
    • 哦!谢谢你。我试过了,但它还在跳。有没有办法将它显示为一个单词而不是一个标签?
    • 嗯,您好像错过了我在 onInitialized 函数中添加的额外代码。试试看:)
    【解决方案2】:

    我从未使用过任何东西滑块,但如果您想更改它的主题标签,您可以开始在脚本中寻找 window.location.hash,因为哈希生成部分将从那里开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多