Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lightgallery无法查看图片 #1

Closed
tiexo opened this issue Aug 9, 2017 · 7 comments
Closed

lightgallery无法查看图片 #1

tiexo opened this issue Aug 9, 2017 · 7 comments

Comments

@tiexo
Copy link

tiexo commented Aug 9, 2017

安装插件后,发现不能点击查看大图了,全部显示的loading图片

qq 20170809220131

主题使用的是lightgallery

@tiexo
Copy link
Author

tiexo commented Aug 11, 2017

我试了fancybox插件依然这样,看来跟点击放大图片冲突了
@Troy-Yang

@Troy-Yang
Copy link
Owner

Hi @tiexo

我能通过Landscape重现你说的fancybox图片问题,看了一下原因,是因为fancybox会在页面加载完成后在所有img外再次包装一层a元素并把img的src复制给其href,如下:

<a href="https://troyyang.com/images/loading.png" title="image" class="fancybox" rel="article1">
     <img src="https://troyyang.com/images/loading.png" data-original="https://images.troyyang.com/2017-6-25-js-inherence.png" alt="image">
</a>

从上可以看到,页面加载后此时所有图片还是使用的loading图片从而导致fancybox把真实图片找错了。而lazyload只有在图片在可视区域时才把img的src替换为真实图片。

作为fancybox的解决方案,可以把https://github.com/hexojs/hexo-theme-landscape/blob/master/source/js/script.js中的97行

$(this).wrap('<a href="' + this.src + '" title="' + alt + '" class="fancybox"></a>');

改为

      $(this).wrap('<a href="' + ($(this).attr("data-original")?$(this).attr("data-original"):this.src) + '" title="' + alt + '" class="fancybox"></a>');

至于lightgallery,我并没有一个在线的hexo demo,但我看了一下那个类库,也看了他的demo,发现和fancybox的问题差不多,解决方案也差不多就是把新增的a元素中的href值替换为data-original中的值,如果你能提供下你的hexo网站地址,我可以帮你具体看看。

类似于在$("#lightgallery").lightGallery(); 执行后运行下面的代码(未测试):

$(document).find('img[data-original]').foreach(  
function(){
 $(this).parent().href = this.attributions.data-original
}  )

希望有帮助。

@tiexo
Copy link
Author

tiexo commented Aug 13, 2017 via email

@ShawyerPeng
Copy link

ShawyerPeng commented Mar 6, 2018

我用的也是lightgallery,按照您的代码在我这儿有语法错误,于是我改成了

$(document).find('img[data-original]').each(function(){
     $(this).parent().href = $(this).attr("data-original");
});

还是不行,所以还是想请教一下您该怎么改才行,谢谢~ @Troy-Yang

@ShawyerPeng
Copy link

不好意思,怪我jQuery不怎么熟悉,代码这样改就OK啦

<script>
    $(document).find('img[data-original]').each(function(){
        $(this).parent().attr("href", $(this).attr("data-original"));
    });
</script>

@Troy-Yang
Copy link
Owner

已经在最新的1.0.6版本里专门对这种问题进行了修复,上面的代码不出意外应该可以删除了

@tiexo
Copy link
Author

tiexo commented Jan 18, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants