PayArticle.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <div class="pay-read-more-wrap"
  3. style="display: none; position: absolute; bottom: 0px; z-index: 9999; width: 100%; margin-top: -100px; font-family: PingFangSC-Regular, sans-serif;">
  4. <div id="pay-read-more-mask"
  5. style="position: relative; height: 200px; background: -webkit-gradient(linear, 0 0%, 0 100%, from(rgba(255, 255, 255, 0)), to(rgb(255, 255, 255)));"></div>
  6. <a id="pay-read-more-btn" target="_blank"
  7. style="position: absolute; left: 50%; top: 70%; bottom: 30px; transform: translate(-50%, -50%); width: 160px; height: 36px; line-height: 36px; font-size: 15px; text-align: center; border: 1px solid rgb(222, 104, 109); color: rgb(222, 104, 109); background: rgb(255, 255, 255); cursor: pointer; border-radius: 6px;">付费阅读</a>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. name: 'PayArticle',
  13. data() {
  14. return {}
  15. },
  16. mounted: function () {
  17. // 延迟执行
  18. setTimeout(() => {
  19. if (this.isPay()) {
  20. let $article = this.articleObj();
  21. this._detect($article, this);
  22. }
  23. }, 150);
  24. // 定时任务
  25. let interval = setInterval(() => {
  26. if (this.isPay()) {
  27. let $article = this.articleObj();
  28. // if ($article && $article.article.hasClass("lock-pay")){
  29. // clearInterval(interval);
  30. // }
  31. this._detect($article, this);
  32. }
  33. }, 1000);
  34. },
  35. methods: {
  36. isPay() {
  37. return this.$page.frontmatter.pay;
  38. },
  39. articleObj: function () {
  40. let $article = $('.theme-default-content');
  41. if ($article.length <= 0) return null;
  42. // 文章的实际高度
  43. let height = $article[0].clientHeight;
  44. return {
  45. article: $article,
  46. height: height
  47. }
  48. },
  49. _detect: function (articleObj, t) {
  50. if (null == articleObj) return;
  51. let $article = articleObj.article;
  52. let height = articleObj.height;
  53. if ($article.length <= 0) return;
  54. // 文章隐藏后的高度
  55. let halfHeight = height * 0.9;
  56. // 判断是否已加锁
  57. if ($article.hasClass("lock-pay")) {
  58. return;
  59. }
  60. // 设置文章可显示高度
  61. $article.css({"height": halfHeight + 'px'});
  62. $article.addClass('lock-pay');
  63. // 删除原有标签
  64. $article.remove("#pay-read-more-wrap");
  65. // 添加加锁标签
  66. let clone = $('.pay-read-more-wrap').clone();
  67. clone.attr('id', 'pay-read-more-wrap');
  68. clone.css('display', 'block');
  69. // 按钮跳转付费
  70. clone.find("#pay-read-more-btn").attr("href", this.$page.frontmatter.pay);
  71. $article.append(clone);
  72. }
  73. }
  74. }
  75. </script>
  76. <style lang="stylus">
  77. #pay-read-more-btn {
  78. border: none !important;
  79. text-decoration: none;
  80. background: #3eaf7c !important;
  81. }
  82. #pay-read-more-btn {
  83. color: #fff !important;
  84. transition: all .5s ease;
  85. }
  86. #pay-read-more-btn:hover {
  87. background: #de3636 !important;
  88. }
  89. .lock-pay {
  90. position: relative;
  91. overflow: hidden;
  92. padding-bottom: 30px;
  93. }
  94. </style>