在较小的屏幕上隐藏 JavaScript

Hide JavaScript at a smaller screen

本文关键字:隐藏 JavaScript 屏幕      更新时间:2023-09-26

我正在将我的网站设置为响应式,我想知道如何在屏幕尺寸小于 700px 时隐藏我的实时聊天 JavaScript。

我目前的实时聊天JavaScript是,

<script type="text/javascript">
function wsa_include_js(){
  var wsa_host = (("https:" == document.location.protocol) ? "https://" : "http://");
  var js = document.createElement("script");
  js.setAttribute("language", "javascript");
  js.setAttribute("type", "text/javascript");
  js.setAttribute("src",wsa_host + "tracking-v3.websitealive.com/3.0/?objectref=wsa3&groupid=12581&websiteid=0");
  document.getElementsByTagName("head").item(0).appendChild(js);
}
if (window.attachEvent)
  window.attachEvent("onload", wsa_include_js);
else if (window.addEventListener)
  window.addEventListener("load", wsa_include_js, false);
else 
  document.addEventListener("load", wsa_include_js, false);
</script>

有人可以告诉我怎么做。谢谢

这实际上可以通过CSS媒体查询轻松解决,但是我需要知道如何将LiveChat添加到HTML中才能给您一个好的答案。

您要做的是获取保存聊天窗口的div 的类或 ID,并将以下内容添加到您的 CSS 文件中:

@media screen and (max-width: 700px) {
    #LiveChatContainerID { display: none; }
}

@media screen and (max-width: 700px) {
    .LiveChatContainerClass { display: none; }
}

如果 LiveChat 要求您将 iframe 添加到您的网站,只需将 iframe 包装在带有唯一 ID 或类的div 标签中,然后在您的 CSS 中使用上述内容即可。

编辑:

看到您的网站后,我想我有一个可以正常工作的解决方案:

@media screen and (max-width: 700px) {
    .wsa_window, .wsa_window_close { display: none !important; }
}

需要"!important"来覆盖javascript直接放在元素上的样式,但是在检查器中执行此操作似乎可以正常工作,而无需删除页面上的任何其他内容。

希望这有帮助!

看看它会帮助你。使用JFiddle很容易

此特定代码会在大小更改时更改颜色,因此您可以简单地根据自己的目的对其进行重组。在代码编辑器中测试您想要实现的目标并查看结果。

function red() {
$('div').css('background','#B60C0C')
.text('Screen Size RED');
}
function orange() {
$('div').css('background','#EBAE10')
.text('Screen Size ORANGE');
}
function green() {
$('div').css('background','#83ba2b')
.text('Screen Size GREEN');
}
var bounds = [
{min:0,max:500,func:red},
{min:501,max:850,func:orange},
{min:851,func:green}
];
var resizeFn = function(){
var lastBoundry; // cache the last boundry used
return function(){
    var width = window.innerWidth;
    var boundry, min, max;
    for(var i=0; i<bounds.length; i++){
        boundry = bounds[i];
        min = boundry.min || Number.MIN_VALUE;
        max = boundry.max || Number.MAX_VALUE;
        if(width > min && width < max 
           && lastBoundry !== boundry){
            lastBoundry = boundry;
            return boundry.func.call(boundry);            
        }
    }
}
};
$(window).resize(resizeFn());
$(document).ready(function(){
$(window).trigger('resize');
});

我也不确定您如何将聊天添加到页面中,但是如果您将其放在div标签中,则可以在较小的屏幕上隐藏该div。

你可以用像这个jsFiddle这样的脚本来做到这一点,但我认为最好使用CSS媒体查询作为Oceanity的答案。

在小提琴中,您可以通过更改输出部分的大小(以手柄为中心)轻松测试它。

(在演示中,大小设置为 400px,以便更轻松地在 jsFiddle 中进行测试。

为了检查大小,我使用了这个SO问题中的脚本。我正在onloadonresize事件中进行尺寸检查。

function getViewPortSize()
{ // source code form here https://stackoverflow.com/questions/10653019/how-to-find-the-screen-width-and-apply-it-to-a-particular-css
    var viewportwidth;
    var viewportheight;
    // Standard browsers
    if (typeof window.innerWidth != 'undefined')
    {
        viewportwidth = window.innerWidth,
        viewportheight = window.innerHeight
    }
    // IE6
    else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
    {
        viewportwidth = document.documentElement.clientWidth,
        viewportheight = document.documentElement.clientHeight
    }
    //Older IE
    else
    {
        viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
        viewportheight = document.getElementsByTagName('body')[0].clientHeight
    }
    return { width: viewportwidth, height: viewportheight};
}
var hideChat = function(evt) {
    console.log(getViewPortSize().width);
    if ( getViewPortSize().width < 400) {
        //console.log('hide div now');
        document.getElementById('chatArea').style = 'display: none';
    }
    else {
        document.getElementById('chatArea').style = 'display: block';
    }
};
window.onresize = function(evt) {
    console.log(evt);
    hideChat(evt);
};
window.onload = function(evt) {
    console.log(evt);
    hideChat(evt);
};
<div id="chatArea">
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,
</div>