简体   繁体   English

如何将背景图像更改添加到jQuery

[英]How to add background-image change to jQuery

I already have the following jQuery to add an accordion-like feature to my mobile view, making the charts open and close on click: 我已经有了以下jQuery,可以在移动视图中添加类似手风琴的功能,从而使图表在单击时打开和关闭:

$('.home-widget h3.widget-title').on('click', function(){

    if(window.matchMedia){
        if(window.matchMedia("only screen and (max-width: 1024px)").matches == true) {
            if($(this).next("div.widget-body").css('display') == "none"){
                $(this).next("div.widget-body").css('display', 'block');
                $(this).parent("div.widget").css('min-height', '648px');
                drawWidgets()
                redrawCharts();
                redrawDatatables();
            }else{
                $(this).next("div.widget-body").css('display', 'none');
                $(this).parent("div.widget").css('min-height', 'auto');
                drawWidgets()
                redrawCharts();
                redrawDatatables();
            }
        }
    }
});

I have a background-image applied to my 'widget-title', which I would like to change from a plus (+) to a minus (-) when the 'widget-body' is displayed. 我将一个背景图像应用于“小工具标题”,当显示“小工具主体”时,我希望将其从加号(+)变为减号(-)。 Could anyone tell me where I would add this to the jQuery provided? 谁能告诉我在哪里将其添加到提供的jQuery中?

CSS is shown here . CSS显示在这里

This should work if I have understood you. 如果我了解您,这应该可以工作。

if($(this).next("div.widget-body").css('display') == "none"){
    $('.widget-title').css('background-image','url(img/1.jpg)');
}else{
    $('.widget-title').css('background-image','url(img/2.jpg)');
}

Do you want it like this? 你想要这样吗?

 if ($(window).width() < 1024 && $('.widget-title').is(":visible")) { $('.widget-title').css('background-image', 'url("https://upload.wikimedia.org/wikipedia/commons/c/ca/Google_Chrome_for_Android_Icon_2016.svg")'); } else { $('.widget-title').css('background-image', 'url("https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png?v=9c558ec15d8a")'); } 
 .widget-title { height: 200px; width: 300px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="widget-title">Testing</div> 

Just try this snippet when you click on "Run code snippet" before you click on "Expand snippet", close it, "Expant snippet" and then "Run code snippet". 在单击“扩展代码段”之前,只需先尝试运行该代码段,然后再单击“扩展代码段”,然后将其关闭,“快速代码段”,然后单击“运行代码段”即可。

You could also create a new class for the background and just add the class to the div . 你也可以创建一个新classbackground ,只是在添加classdiv

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM