Twikoo添加图片点击放大功能

转自 为 butterfly 主题的 twikoo 添加图片点击放大功能仅为学习记录。

主题目录\layout\includes\third-party\comments
修改 twikoo.pug,大概在第 5 行左右的位置。

twikoo.init(Object.assign({
  el: '#twikoo-wrap',
  envId: '!{theme.twikoo.envId}',
  region: '!{theme.twikoo.region}',
  onCommentLoaded: function () {
    if(typeof window.mediumZoom!='undefined'){
      const zoomComment = mediumZoom(document.querySelectorAll('.tk-content :not(a)>img'))
      zoomComment.on('open', e => {
        const photoBg = document.documentElement.getAttribute('data-theme') === 'dark' ? '#121212' : '#fff'
        zoomComment.update({
          background: photoBg
        })
      })
    }
    if(GLOBAL_CONFIG.lightbox === 'fancybox'){
        const addFancybox = function (ele) {
          const runFancybox = (ele) => {
            ele.each(function (i, o) {
              const $this = $(o)
              const lazyloadSrc = $this.attr('data-lazy-src') || $this.attr('src')
              const dataCaption = $this.attr('alt') || ''
              $this.wrap(`<a href="${lazyloadSrc}" data-fancybox="group" data-caption="${dataCaption}" class="fancybox"></a>`)
            })

            $().fancybox({
              selector: '[data-fancybox]',
              loop: true,
              transitionEffect: 'slide',
              protect: true,
              buttons: ['slideShow', 'fullScreen', 'thumbs', 'close'],
              hash: false
            })
          }

          if (typeof $.fancybox === 'undefined') {
            $('head').append(`<link rel="stylesheet" type="text/css" href="${GLOBAL_CONFIG.source.fancybox.css}">`)
            $.getScript(`${GLOBAL_CONFIG.source.fancybox.js}`, function () {
              runFancybox($(ele))
            })
          } else {
            runFancybox($(ele))
          }
        }
      addFancybox(document.querySelectorAll('.tk-content :not(a)>img'))
    }
  }
}, !{JSON.stringify(theme.twikoo.option)}))

完整版

script.
  (()=>{
    const $countDom = document.getElementById('twikoo-count')
    const init = () => {
      twikoo.init(Object.assign({
        el: '#twikoo-wrap',
        envId: '!{theme.twikoo.envId}',
        region: '!{theme.twikoo.region}',
        onCommentLoaded: function () {
          if (typeof window.mediumZoom != 'undefined') {
            const zoomComment = mediumZoom(document.querySelectorAll('.tk-content :not(a)>img'))
            zoomComment.on('open', e => {
              const photoBg = document.documentElement.getAttribute('data-theme') === 'dark' ? '#121212' : '#fff'
              zoomComment.update({
                background: photoBg
              })
            })
          }
          if (GLOBAL_CONFIG.lightbox === 'fancybox') {
            const addFancybox = function (ele) {
              const runFancybox = (ele) => {
                ele.each(function (i, o) {
                  const $this = $(o)
                  const lazyloadSrc = $this.attr('data-lazy-src') || $this.attr('src')
                  const dataCaption = $this.attr('alt') || ''
                  $this.wrap(`<a href="${lazyloadSrc}" data-fancybox="group" data-caption="${dataCaption}" class="fancybox"></a>`)
                })

                $().fancybox({
                  selector: '[data-fancybox]',
                  loop: true,
                  transitionEffect: 'slide',
                  protect: true,
                  buttons: ['slideShow', 'fullScreen', 'thumbs', 'close'],
                  hash: false
                })
              }

              if (typeof $.fancybox === 'undefined') {
                $('head').append(`<link rel="stylesheet" type="text/css" href="${GLOBAL_CONFIG.source.fancybox.css}">`)
                $.getScript(`${GLOBAL_CONFIG.source.fancybox.js}`, function () {
                  runFancybox($(ele))
                })
              } else {
                runFancybox($(ele))
              }
            }
            addFancybox(document.querySelectorAll('.tk-content :not(a)>img'))
          }
        }
      }, !{JSON.stringify(theme.twikoo.option)}))
    }
    const getCount = () => {
      twikoo.getCommentsCount({
        envId: '!{theme.twikoo.envId}',
        region: '!{theme.twikoo.region}',
        urls: [window.location.pathname],
        includeReply: false
      }).then(function (res) {
        $countDom.innerText = res[0].count
      }).catch(function (err) {
        console.error(err);
      });
    }

    const loadTwikoo = (bool = false) => {
      if (typeof twikoo === 'object') {
        init()
        bool && $countDom && setTimeout(getCount,0)
      } else {
        getScript('!{theme.CDN.twikoo}').then(()=> {
          init()
          bool && $countDom && setTimeout(getCount,0)
        })
      }
    }

    if ('!{theme.comments.use[0]}' === 'Twikoo' || !!{theme.comments.lazyload}) {
      if (!{theme.comments.lazyload}) btf.loadComment(document.getElementById('twikoo-wrap'), loadTwikoo)
      else loadTwikoo(true)
    } else {
      window.loadOtherComment = () => {
        loadTwikoo()
      }
    }
  })()
Invitation
HuRan
854262623
created:04/03/2022
Welcome to Candyhome

Use this card to join the candyhome and participate in a pleasant discussion together .

Welcome to Knlde's candyhome,wish you a nice day .

© 版权声明
THE END
喜欢就支持一下吧
点赞0赞赏 分享
评论 抢沙发

请登录后发表评论