WordPress 网站添加弹窗广告代码

我们平时访问网站、博客时经常会打开网页后看到弹出一个图片广告。如果作为普通访客可能会略有抵触,但是作为站长,我们却十分需要这样的广告来为网站赚钱贴补一下服务器维护费用。

1、修改 JavaScript 代码:

  1. var popup = document.getElementById(‘qgg_popup‘);
  2. var popup_box = document.querySelector(‘.qgg_popup_box‘);
  3. var popup_close = document.querySelector(‘.qgg_popup_close‘);
  4. // 窗口加载时弹出
  5. window.onload = function() {
  6.  popup.style.display = "block";
  7. }
  8. // 点击窗体其他位置时关闭
  9. window.onclick = function(event) {
  10.  if (event.target == popup) {
  11.  popup.style.display = "none";
  12.  }
  13. }
  14. popup_box.onclick = function() {
  15.  popup.style.display = "none";
  16. }
  17. // 点击关闭按钮时关闭
  18. popup_close.onclick = function() {
  19.  popup.style.display = "none";
  20. }

 

使用 wordpress 建站的朋友将 JS 代码丢到主题的主 JS 文件中去即可。DUX 主题用户直接丢到主题 js 文件夹下的 main.js 文件中即可。其他程序建站的朋友可以放到自己相应的 JS 文件里。

2、修改 CSS 样式代码:

  1. /* 弹窗广告css 2018-8-29 */
  2. html, body{ margin:0; height:100%; }
  3. #qgg_popup{
  4.  position: fixed;
  5.  top: 0; left: 0;
  6.  display: none;
  7.  width: 100%;
  8.  height: 100%;
  9.  margin: auto;
  10.  background: rgba(36, 36, 36, 0.8);
  11. }
  12.  
  13. .qgg_popup_box { 
  14.  z-index: 10; 
  15.  position: absolute;
  16.  top: 0; left: 0; bottom: 0; right: 0;
  17.  width: 280px;
  18.  height: 396px;
  19.  margin: auto;
  20.  text-align: center; 
  21. } 
  22. .qgg_popup_close{
  23.  position: relative;
  24.  width: 36px;
  25.  height: 36px;
  26.  background: #fff;
  27.  color: #999;
  28.  float: right;
  29.  font-size: 24px;
  30.  text-align: center;
  31.  border-radius: 50%;
  32.  line-height: 36px;
  33.  font-weight: bold;
  34. }
  35.  
  36. .qgg_popup_close:hover,
  37. .qgg_popup_close:focus {
  38.  color: red;
  39.  cursor: pointer;
  40. }
  41.  
  42. .qgg_popup_img{
  43.  position:relative;
  44.  top: 36px;
  45.  left: 0px;
  46.  width:240px;
  47.  height:360px;
  48.  border-radius: 15px;
  49. }
  50. @media (max-width: 640px){
  51.  .qgg_popup_close{ display: none; }
  52. }

使用 WordPress 搭建网站的朋友将代码丢到主题的 style.css 文件中即可。DUX 主题丢到 main.css 文件中即可。使用其他网站程序的添加到相应的 css 文件中。

3、修改 html 代码:

这段代码是前端显示的 HTML ,将其放到你想要其显示的页面中即可,比如首页文件 index.php 、文章文件 single.php 中即可。

  1. <!-- 弹窗广告 -->
  2. <div id="qgg_popup">
  3.  <div class="qgg_popup_box">
  4.  <span class="qgg_popup_close">&times;</span>
  5.  <img class="qgg_popup_img" src="./1.png">
  6.  </div>
  7. </div>

注意,代码中“./1.png”这里需要修改成你自己的弹窗广告图片地址。这样就可以实现为网站添加弹窗广告的功能了,有网站需要弹窗功能的朋友可以自己试一下。可能这样小小的修改有时候就能为你网站赚钱增加一个新的渠道!

相关文章