【问题标题】:Cannot Load URL from Hash / Anchor无法从哈希/锚点加载 URL
【发布时间】:2012-08-21 12:50:54
【问题描述】:

这是我的问题:

我用 Jquery Elastislide 插件创建了一个画廊

当我点击拇指时,Hash 出现在图片中......但是当我刷新/重新加载页面作为链接时,Hash 没有加载图片,我希望 Hash 加载相应的图片。

我尝试了一些 Jquery 插件,例如 Address、BBQ、History,但它不起作用。

$(function() {

    $.fn.imagesLoaded       = function( callback ) {
    var $images = this.find('img'),
        len     = $images.length,
        _this   = this,
        blank   = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';


    function triggerCallback() {
        callback.call( _this, $images );
    }

    function imgLoaded() {
        if ( --len <= 0 && this.src !== blank ){
            setTimeout( triggerCallback );
            $images.off( 'load error', imgLoaded );
        }
    }

    if ( !len ) {
        triggerCallback();
    }

    $images.on( 'load error',  imgLoaded ).each( function() {
        // cached images don't fire load sometimes, so we reset src.
        if (this.complete || this.complete === undefined){
            var src = this.src;
            // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
            // data uri bypasses webkit log warning (thx doug jones)
            this.src = blank;
            this.src = src;
        }
    });




    return this;
    };


    // gallery container
    var $rgGallery          = $('#rg-gallery'),
    // carousel container
    $esCarousel         = $rgGallery.find('div.es-carousel-wrapper'),
    // the carousel items
    $items              = $esCarousel.find('ul > li'),
    // total number of items
    itemsCount          = $items.length;



    Gallery             = (function() {
            // index of the current item




        var  current            =  0,


            // mode : carousel || fullview
            mode            = 'carousel',
            // control if one image is being loaded
            anim            = false,
            init            = function() {

                // (not necessary) preloading the images here...
                $items.add('<img src="ajax-loader.gif"/><img src="black.png"/>').imagesLoaded( function() {
                    // add options
                    _addViewModes();

                    // add large image wrapper
                    _addImageWrapper();

                    // show first image
                    _showImage( $items.eq(window.location.hash));

                });

                // initialize the carousel
                if( mode === 'carousel' )
                    _initCarousel();

            },
            _initCarousel   = function() {

                // we are using the elastislide plugin:
                // http://tympanus.net/codrops/2011/09/12/elastislide-responsive-carousel/
                $esCarousel.show().elastislide({
                    imageW  : 65,
                    onClick : function( $item ) {
                        if( anim ) return false;
                        anim    = true;
                        // on click show image
                        _showImage($item);
                        // change current
                        current = $item.index(location.hash);
                        that.attr('href')

                    }
                });

【问题讨论】:

  • 刷新/重新加载行为会有所不同,具体取决于您在做什么以及您所在的浏览器。最初,如果页面已经加载,则如果存在哈希值,浏览器将不会重新加载页面他们只会滚动到它。在大多数浏览器中按CTRL + F5 将强制无条件重新加载,这应该重新加载页面。
  • 我明白了。事实上,我想从 Hash 中获得一个单独的链接。当手动刷新或重新加载页面时,会出现Hash对应的图片。例如,我可以在 Facebook 上发布我的画廊的特定图像的链接。但我没有找到解决这个问题的方法。

标签: jquery url hash anchor responsive-design


【解决方案1】:

所以如果有哈希,我们希望在页面加载时将信息传递给弹性幻灯片。

var imageIndex = 0; 
if (window.location.hash) {
    var imageIndexStr = window.location.hash.replace('#',''); // remove #
    imageIndex = parseInt(imageIndexStr, 10); // convert to int
}
$esCarousel.show().elastislide({
    current : imageIndex,
    [rest of elastislide setup as above]
});

我在这里假设哈希由图像的索引组成,例如#3

【讨论】:

  • 非常感谢您的回答 Owlvark。
  • 非常感谢您的回答 Owlvark。我认为这个代码提供了很好的答案。我尝试使用您的代码,当“www.example.com/gallery.html”加载时,画廊的第一张图片出现(很好)..但是当“www.example.com/gallery.html#title”加载时,图库仍在加载中,相应的图片没有出现..
  • 我认为解决方案是var imageIndexStr = window.location.hash.replace('#',''); 因为当我更改为var imageIndexStr = window.location.hash('#',''); 时,画廊不会加载。
  • 所以你找到了解决方案,我忘了把数字放在 Hash 上!非常感谢:)
猜你喜欢
  • 2011-04-26
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 2016-05-18
  • 2022-11-10
  • 2011-06-27
  • 2022-08-24
相关资源
最近更新 更多