搭建Hexo博客相册

2018-09-02  本文已影响317人  MaLizhi

图片的处理

- with open("E://YourBlog/themes/next/source/lib/album/data.json","w") as fp:
+ with open("YourBolgPath/themes/next/source/lib/album/data.json","w") as fp:

博客相册页面

menu:
  home: / || home
  about: /about/ || user
  categories: /categories/ || th-list
  tags: /tags/ || tags
  archives: /archives/ || archive
  #schedule: /schedule/ || calendar
  音乐: /music/ || music
+ 相册: /photos  || camera

在photos中的index.md添加上下面代码

---
title: 照片
date: 2018-09-02 21:00:00
type: photos
fancybox: false
comments: true
---
<link rel="stylesheet" href="../lib/album/ins.css">
<link rel="stylesheet" href="../lib/album/photoswipe.css"> 
<link rel="stylesheet" href="../lib/album/default-skin/default-skin.css"> 
<div class="photos-btn-wrap">
  <a class="photos-btn active" href="javascript:void(0)" target="_blank" rel="external">Photos</a>
</div>
<div class="instagram itemscope">
  <a href="http://yourbolg.com" target="_blank" class="open-ins">图片正在加载中…</a>
</div>
 
<script>
  (function() {
    var loadScript = function(path) {
      var $script = document.createElement('script')
      document.getElementsByTagName('body')[0].appendChild($script)
      $script.setAttribute('src', path)
    }
    setTimeout(function() {
        loadScript('../lib/album/ins.js')
    }, 0)
  })()
</script>

从index就可以看出我们还需要添加css文件和js文件,这些文件我已经上传到GitHub中的JS&CSS文件夹中,在这里需要把这个文件夹中的所有文件包括此文件夹下的所有目录都拷贝到前面创建的/hexo/themes/next/source/lib/album中。

JS&CSS文件夹中的内容如下:

image
然后再将photoswipe-ui-default.min.js, photoswipe.min.js两个文件添加到themes/next/source/js/src中。

对ins.js(ins.js在album文件夹中)文件进行一个简单的修正,将里面大约在121和122行左右的连接修改为你的GitHub中图片连接地址

-    var minSrc = 'https://raw.githubusercontent.com/lizhi/Blog_Album/master/min_photos/' + data.link[i];
-    var src = 'https://raw.githubusercontent.com/lizhi/Blog_Album/master/photos/' + data.link[i];
+    var minSrc = 'https://raw.githubusercontent.com/yougithubname/pathtoyourphotos/min_photos/' + data.link[i];
+    var src = 'https://raw.githubusercontent.com/yougithubname/pathtoyourphotos/photos/' + data.link[i];

确保你的图片地址是正确的,否则加载不出来,检测方法可以将选择一个图片地址看能不能再浏览器中打开。

<blockquote>注意点:图片的链接都是https://raw.githubusercontent.com 开头的,不是图片的存储地址,否则加载不出</blockquote>

其他配置

<script src="{{ url_for(theme.js) }}/src/photoswipe.min.js?v={{ theme.version }}"></script>
<script src="{{ url_for(theme.js) }}/src/photoswipe-ui-default.min.js?v={{ theme.version }}"></script>

在body中添加如下的内容:

{% if page.type === "photos" %}
  <!-- Root element of PhotoSwipe. Must have class pswp. -->
  <div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="pswp__bg"></div>
    <div class="pswp__scroll-wrap">
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
  </div>
{% endif %}
<script type="text/javascript" src="{{ url_for(theme.js) }}/src/photoswipe.min.js?v={{ theme.version }}"></script>
<script type="text/javascript" src="{{ url_for(theme.js) }}/src/photoswipe-ui-default.min.js?v={{ theme.version }}"></script>
+{% if page.type === "photos"  %}
+  {% set theme.fancybox = false %}
+{% endif %}
+{% if page.type !== "photos"  %}
+  {% set theme.fancybox = true %}
+{% endif %}

{% if theme.fancybox  %}
  {% set fancybox_css_uri = url_for(theme.vendors._internal + '/fancybox/source/jquery.fancybox.css?v=2.1.5') %}
  {% if theme.vendors.fancybox_css %}
    {% set fancybox_css_uri = theme.vendors.fancybox_css %}
  {% endif %}
  <link href="{{ fancybox_css_uri }}" rel="stylesheet" type="text/css" />
{% endif %}

至此所有的配置已经完成,赶快部署发布吧。

上一篇下一篇

猜你喜欢

热点阅读