在页脚中使用浮动/固定的div

4
我有一个WordPress网站,我想仅在移动设备上的页面底部显示1个Google Adsense横幅广告(320px x 50px)。当我说页面底部时,我应该真正指的是可视屏幕的底部,这样无论用户在页面上的任何位置,都可以看到它。
我已经确定需要使用媒体查询,并查找了使用该网站的主要移动设备的大小,它们的宽度为320px或360px。
我为此编写了CSS,并将其放置在标准的style.css文件中;
@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 360px) {
.mobileadunit {
  width:320px;
  height:50px;
  position:fixed;
  bottom:0px;
  }

}

我随后将 div 放置在 WordPress 的 footer.php 文件中,就在 </body> 标签之前。

<div class="mobileadunit">
ADSENSE CODE
</div>

尽管我已经做了最大的努力,但是我仍然不能让它显示出来。我是否犯了一些十分明显的错误?


“所以无论用户在页面上的位置如何,它始终可见。”请注意:在网页中滚动、悬停或扩展广告的“浮动框”中放置Google广告是不可接受的。目前禁止使用粘性广告。 - Avatar
3个回答

3
原来,仅使用 CSS "@media queries" 可能无法实现您想要的结果或最佳用户体验。
首先,我不是专家,更多的是学习者。无论如何,目前看来 Google AdSense 广告是固定大小的,并且在不同设备上查看时不会适应不同的屏幕宽度。在浏览响应式网站时,在较小的设备上,AdSense 广告会扩展到其容器之外,并且经常超出屏幕 - 破坏布局,降低用户体验,甚至使移动访问者无法点击您的广告。
那么,有没有解决方法呢?
是的,如果您搜索网络,您会发现一些解决方案,包括 Google 对 AdSense 代码进行修改的官方建议。我已经遇到过这个问题一次,经过快速的研究,我知道了两个非常好的插件,可以在响应式 WordPress 网站上显示 AdSense 广告。请参见 Google AdSense for Responsive Design Plugin(免费选项)和 Easy Responsive AdSense Plugin(付费选项)。

如前所述,Google Adsense计划政策涵盖了他们自己的修改Adsense代码的方法和指导方针,虽然他们接受的修改指南非常简单明了且强烈推荐,只要不以有害于程序的方式修改Adsense代码,则Google可以接受对Adsense代码进行轻微修改而不对用户帐户施加任何惩罚。

无论是使用插件还是不使用插件,JavaScript + CSS媒体查询都是实现更好用户体验的最佳方法。如果您不想依赖插件,则可以按照以下步骤,只需使用一些CSS和JS即可让其正常工作。

解决方案:

根据前述插件的解决方案,我建立了一个快速demonstration (现场测试网站),以便您可以在您的手机上访问/测试。演示网站仅在移动手机上显示AdSense移动横幅(320x50像素),并通过使用媒体查询检测其屏幕尺寸来显示其他具有不同大小格式的广告单元,然后通过非常简单的javascript指示/告诉Google在加载页面时选择最佳广告尺寸进行显示。
请记住,该代码在页面首次加载时是响应式的,但对于窗口大小或设备方向的任何后续更改都不会响应。浏览演示网站时,请考虑每次更改窗口大小时刷新页面。
此外,您也可以在CODEPEN上进行操作,但请记住,为使其正常工作,您应该用自己的AdSense客户端号和广告位号替换它。 这里是它在iOS 7移动safari iPhone 5上运行的几张截图

让我们把它分解成几个部分:

首先,设置HTML标记:

AdSense移动横幅和其他不同尺寸的广告单元将根据具有id="responsive-adsense"<div>的当前宽度进行渲染,该

位于id="adcontainer"
中,这是我们将始终固定在底部放置横幅的容器。

<div class="adcontainer">

<div id="responsive-adsense">
<!-- AdSense javascript goes here -->
</div>

</div>

然后立即嵌入AdSense的javascript代码:

在这里,我已经注释了好的提示。

<div class="adcontainer">
<div id="responsive-adsense">

 <script type="text/javascript">
  // Replace google_ad_client number with ith your AdSense Publisher ID
  google_ad_client = "ca-pub-928637890363315";
  // Detect and calculate the width of the "<div>" where AdSense ads will be rendered
  var containerWidth = document.getElementById( "responsive-adsense" ).offsetWidth;
  // If ad container is 728px...
   if ( containerWidth >= 728 ) {
  //...Then, display Adsense ad of size 728x90px
  // Remember to replace the AdSense Ad Slot ID as well for all the different size ad units
  google_ad_slot = "2385224440";
  google_ad_width = 728;
  google_ad_height = 90;
    }
  // If ad container is 468px, then display Adsense ad of size 468x60px
   else if ( containerWidth >= 468 ) {
  google_ad_slot = "1350406442";
  google_ad_width = 468;
  google_ad_height = 60;
   }
  // If ad container is 320px, then display Adsense ad of size 320x50px
   else if ( containerWidth >= 320 ) {
  // It is even possible to set a different google_ad_client ID only for your mobile traffic
  google_ad_client = "ca-pub-928637849363315";
  google_ad_slot = "1350806442";
  google_ad_width = 320;
  google_ad_height = 50;
   }
  // If ad container is 234px, then display Adsense ad of size 234x60px
   else if ( containerWidth >= 234 ) {
  google_ad_slot = "2825039647";
  google_ad_width = 234;
  google_ad_height = 60;
   }
</script>

<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

</div>
</div>

上面的javascript代码涵盖了4个特定的广告单元。谷歌建议不要修改AdSense代码以在同一页面上显示超过3个广告单元,因此建议仅在唯一页面中设置3个广告单元。只要根据设备屏幕大小在唯一页面中显示1个广告,我们就可以开始了。请花些时间阅读Google的AdSense政策here
我们使用上面的javascript代码覆盖了4个具有不同尺寸的广告单元,如下所示:
- 234 x 60 - 半横幅 - 320 x 50 - 移动横幅 - 468 x 60 - 横幅 - 728 x 90 - 排版广告
现在,借助媒体查询的css帮助,我们将针对最常见的小屏幕尺寸进行目标定位。
我们将介绍一种扩展方法,通过下面的CSS媒体查询来显示AdSense广告以适应各种屏幕大小,从而针对大多数移动设备,如智能手机和平板电脑。我们在这里并不严格针对任何特定的移动设备,因此您可以始终构建自己的定位方案,或者仅使用Adsense Mobile横幅(320x50)代码部分,就像在这种方法中所示的那样...
根据您的问题,您不想在桌面浏览器上显示它,因此当在桌面上浏览网站时,我们会首先设置display: none;为所有内容。
/* Default Stylesheet */

#responsive-adsense {
  display: none; 
}

#responsive-adsense{
  display: none;
} 


/*
GENERAL MEDIA QUERY FOR SMALL SCREEN SIZES - COVERS COMMON MOBILE DEVICES, PHONES/TABLETS...
*/

@media only screen and (max-width: 1023px) {

.adcontainer {
  display: none; 
}

#responsive-adsense{
  display: none;
} 

}


@media only screen and (max-width: 899px) {

.adcontainer {
  width: 100%;
  height: auto;
  position: fixed;
  bottom: 0px;
  display: block;
  background: #e74c3c;
  color: #fff;
  margin: 0 auto;
  padding: 5px;
 }

#responsive-adsense {
  width: 728px !important;
  display: block !important;
}

}


@media only screen and (max-width: 767px) {

.adcontainer {
 width: 100%;
 height: auto;
 position: fixed;
 bottom: 0px;
 display: block;
 background: #e74c3c;
 color: #fff;
 margin: 0 auto;
 padding: 5px;
}

#responsive-adsense {
 width: 728px !important;
 display: block !important; 
}

}


@media only screen and (max-width: 599px) {

.adcontainer {
 width: 100%;
 height: auto;
 position: fixed;
 bottom: 0px;
 display: block;
 background: #e74c3c;
 color: #fff;
 margin: 0 auto;
 padding: 5px;
}

#responsive-adsense {
 width: 468px !important;
 display: block !important; 
}

}


@media only screen and (max-width: 479px) {

.adcontainer {
 width: 100%;
 height: auto;
 position: fixed;
 bottom: 0px;
 display: block;
 background: #e74c3c;
 color: #fff;
 margin: 0 auto;
 padding: 5px;
}

#responsive-adsense {
 width: 320px !important;
 display: block !important;
} 

}

/* Here's the css for mobile devices */  
@media only screen and (max-width: 320px) {

.adcontainer {
 width: auto !important; 
 padding: 0px !important;
 height: 50px !important; 
}

#responsive-adsense {
 width: 320px !important;
 display: block !important; 
}

}

最后但同样重要的是,您始终可以遵循谷歌的响应式方法,在网站上设置AdSense广告,并进行自己的更改,只要不侵犯其政策中禁止使用的任何条款。https://support.google.com/adsense/answer/1354736?hl=en&topic=1271508 这可能不是最适合您的解决方案,但即使谷歌也指出他们的尝试还不完美,并在您认为有必要时接受您自己的修改,再次强调,对AdSense代码的任何修改都应符合Google Adsense政策的良好实践。
其他参考资料:
以下文章介绍了两种不同的方法,以正确地在响应式网站上显示AdSense广告。本回答的大部分内容均来自这些来源,以及谷歌的官方建议。
请查看,它可能非常有用:

http://www.labnol.org/internet/google-adsense-responsive-design/25252/

http://www.akshitsethi.me/google-adsense-responsive-ads-explained/

我希望这能帮助你和社区中的其他人。欢迎提出任何改进意见。


这个解决方案实现了我所需的,而且还有更多!感谢您提供如此全面的解释,我已经实施了您的建议,它完美地运行。我唯一做的就是删除容器的背景颜色,这样如果广告较小或没有广告,则可以看到其后面的页面。 - Jonathan Rosenfeld
嘿@JonathanRosenfeld - 很高兴听到它对你有用!是的,在演示页面中设置了背景颜色,只是为了突出广告容器。无论如何,这确实是一个扩展答案,似乎存在一些不一致之处,但经过一些更正,可以使其对其他成员更加清晰。如果这个答案真的对你有帮助,请记得给它投票。祝你在项目中好运。那时见! - Adriano Monecchi

2
@media only screen and (min-device-width : 320px) and (max-device-width : 480px) {
  .mobileadunit {
      width:320px;
      height:50px;
      position:fixed;
      bottom:0px;
      display:block;
  }
}

我已经将媒体查询从min/max-device-width更改为min/max-width,以便它可以在计算机屏幕上工作。这是一个jsfiddle的演示链接:

http://jsfiddle.net/hm3Cj/

如果您感兴趣,以下是有关仅min/max-width和min/max-device-width之间差异的优秀文章:http://www.javascriptkit.com/dhtmltutors/cssmediaqueries2.shtml。此外,该文章还包含了所有常见的设备/屏幕尺寸。您目前使用的显示空间相对较小,320 x 480可能更适合移动电话。


0

已解决:这对我来说运行良好,已在移动设备底部修复了Google AdSense。

HTML

<div class="main_ad">
<div class="mobileadunit">
    <!-- Put Code Given By Google Adsense -->
    <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXX"
            crossorigin="anonymous"></script>
    <ins class="adsbygoogle"
         style="display:inline-block;width:320px;height:50px"
         data-ad-client="ca-pub-XXXXXXXXXXX"
         data-ad-slot="XXXXXXXXXXX"></ins>
    <script>
        (adsbygoogle = window.adsbygoogle || []).push({});
    </script>
</div>
</div>

CSS

.main_ad{
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  z-index: 101;
  display: block;
}

.mobileadunit {
  display: none;
  margin:0px auto;
}

@media only screen and (min-width : 320px) and (max-width : 480px) {
  .mobileadunit {
    width:320px;
    height:50px!important;
    bottom:0;
    display:block!important;
    z-index: 1;
  }
}

排序

<div class="main_ad">
   <div class="mobileadunit">
    ADSENSE CODE
   </div>
</div>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接