简体   繁体   English

如何使广告仅对移动用户可见?

[英]How can I make an ad visible only to mobile users?

I want to use some mobile ads but my website has responsive design so I don't use the m.我想使用一些移动广告,但我的网站有响应式设计,所以我不使用 m。 subdomain.子域。 How can I make an ad visible only to mobile users?如何使广告仅对移动用户可见? What about only to desktop or tablet?仅适用于台式机或平板电脑呢?

I'm using wordpress as a CMS.我正在使用 wordpress 作为 CMS。

Thanks,谢谢,

It's quite easy using CSS3 Media Queries .使用CSS3 Media Queries非常容易。

In your CSS:在您的 CSS 中:

@media (max-width: 480px) {
      #ad-id {
         display: block;
      }
}

For bigger devices, hide it:对于更大的设备,隐藏它:

@media (min-width: 480px) {
     #ad-id {
        display: none;
     }
}

If you are really concerned about data being downloaded, then you must detect the browser size on page load, if it's less than particular width then fire an ajax call and fetch the ad data and display it inside the placeholder container.如果您真的担心正在下载的数据,那么您必须在页面加载时检测浏览器大小,如果它小于特定宽度,则触发 ajax 调用并获取广告数据并将其显示在占位符容器中。

$(function() {
    if($(window).width() < 480) {
       $("#ad-id").load("adcontent.html");
       // else use $.ajax for this purpose
    }
});

Please use css media queries.请使用 CSS 媒体查询。 you can show and hide specific div for mobile devices.您可以显示和隐藏移动设备的特定 div。

 @media only screen 
  and (min-device-width: 320px) 
  and (max-device-width: 480px)
  and (-webkit-min-device-pixel-ratio: 2) { }

One way is to use media queries.一种方法是使用媒体查询。 Make the ads as display:none by default and add display:block only in media queries for mobile.将广告设为 display:none 默认情况下,仅在移动媒体查询中添加 display:block。

.ads{
  display:none;
}
@media (max-width:480px){
.ads{
  display:block;
}
} 

Despite the seeming simplicity is quite a tricky question .尽管看似简单是一个相当棘手的问题

Try not to waste time (which has already been spent by other developers) and use one of the existing solutions like isMobile or mobile-detect.js .尽量不要浪费时间(其他开发人员已经花费了这些时间)并使用现有的解决方案之一,例如isMobilemobile-detect.js

This libraries allows you to detect:该库允许您检测:

  • is mobile, tablet or desktop ;是移动设备、平板电脑或台式机
  • specific browser version.特定浏览器版本。
  • operating system;操作系统;
  • ... ...

You can do it using CSS3 media queries,你可以使用 CSS3 媒体查询来做到这一点,

//medium+ screen sizes //中等+屏幕尺寸

@media (min-width:992px) { @media(最小宽度:992px){

.desktop-only {
    display:block !important;
}

} }

//small screen sizes //小屏幕尺寸

@media (max-width: 991px) { @media(最大宽度:991px){

.mobile-only {
    display:block !important;
}

.desktop-only {
    display:none !important;
}

} }

for large resolution devices you can use following media queries,对于大分辨率设备,您可以使用以下媒体查询,

//large resolutions only //仅大分辨率

@media (min-width: 1200px) { ... } @media(最小宽度:1200px){ ... }

Also if you are using any front-end framework like bootstrap.此外,如果您使用任何前端框架,如引导程序。 then you can find some written classes like然后你可以找到一些书面课程,比如

.visible-phone .visible-tablet .visible-phone .visible-tablet

.visible-desktop .visible-desktop

.hidden-phone .hidden-phone

.hidden-tablet .hidden-tablet

.hidden-desktop .hidden-desktop

using these classes you can play around your content to show and hide on specific devices.使用这些类,您可以播放您的内容以在特定设备上显示和隐藏。

http://getbootstrap.com/2.3.2/scaffolding.html http://getbootstrap.com/2.3.2/scaffolding.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使组件仅在少数用户集中可见/可用? - How to make a component visible/available only for few set of users in react? 如何使某些内容仅对管理员/ mod用户可见 - How to make some content visible only to admin/mod users Javascript 如何制作仅适用于移动设备的幻灯片 - Javascript how can i make slideshow only for mobile 如何才能使<a href>仅在移动设备上有效? - How can I make an <a href> only active on mobile devices? 如何更改此JS以使其可见? - How can I change this JS to make it visible? 如何使仅在X分钟后可见的javascript / jQuery倒计时? - How can I make a javascript/jQuery countdown that is only visible after X minutes? 如何仅使KendoUI dataviz折线图中的最后一个标记可见? - How can I make only the last marker in a KendoUI dataviz line chart visible? 如何使该“ div”始终对用户可见 - how to make this “div” always visible to users 在Bootstrap JS Modal中,如何使Submit按钮仅对移动应用程序可见 - In Bootstrap JS Modal, how to make Submit button only visible for mobile application (discord.js) 如何使此命令仅适用于某些角色/用户? - (discord.js) How can I make this command only work for certain roles/users?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM