Typecho添加图片暗箱效果

其他 4024

文章最后更新时间为:2020 年 05 月 07 日 22:55:54

1、引用 FancyBox插件

<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<!--如果主题已经引用了jQuery库,可以忽略这条-->
<link rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.2/jquery.fancybox.min.css">
<script src="https://cdn.staticfile.org/fancybox/3.5.2/jquery.fancybox.min.js"></script>

2、给文章图片添加标签
找到post.php里的

<?php $this->content(); ?>

替换成

<?php
    $pattern = '/\<img.*?src\=\"(.*?)\"[^>]*>/i';
    $replacement = '<a href="$1" data-fancybox="gallery" /><img src="$1" alt="'.$this->title.'" title="点击放大图片"></a>';
    $content = preg_replace($pattern, $replacement, $this->content);
    echo $content;
?>

3、在头部或者尾部初始化

<script type="text/javascript">
    $(document).ready(function () {
        $( ".fancybox").fancybox();
    });
</script>

就可以了

none
打赏采用二维码方式。

已有 4 条评论

  1. 非常感谢

  2. 我就看一下 反正也学不会 哈哈

    1. 其实很简单的,网上找到的最简单粗暴的方法

  3. 这么全面的教程,赞一个

添加新评论

取消回复