想要点击事件更改背景图像

时间:2016-11-06 15:45:55

标签: jquery html css wordpress

我有一个页面,我已经放了三个标签。单击每个选项卡后,相关文本将更改。我想为每个选项卡放置三个图像,因此当用户单击选项卡时,图像应该更改。 分享主题链接:

https://www.themographics.com/wordpress/docdirect/

以下是每次点击时标签在标签上的更改方式。共享JS

//交换标题

jQuery(document).on('click','.current-directory',function(){    
        jQuery(this).parents('ul').find('li').removeClass('active');
        jQuery(this).addClass('active');
        var dir_name    = jQuery(this).data('dir_name');
        var id  = jQuery(this).data('id');
        jQuery(this).parents('.tg-banner-content').find('em.current_directory').html(dir_name);

        if( Z_Editor.elements[id] ) {
            var load_subcategories = wp.template( 'load-subcategories' );
            var data = [];
            data['childrens']    = Z_Editor.elements[id];
            data['parent']      = dir_name;
            var _options    = load_subcategories(data);
            jQuery( '.subcats' ).html(_options);
        }

    });

    //Prepare Subcatgories
    jQuery(document).on('change','.directory_type', function (event) {
        var id        = jQuery('option:selected', this).attr('id');     
        var dir_name    = jQuery(this).find(':selected').data('dir_name');

        if( jQuery( '.dynamic-title' ).length ){
            jQuery( '.dynamic-title' ).html(dir_name);
        }

        if( Z_Editor.elements[id] ) {
            var load_subcategories = wp.template( 'load-subcategories' );
            var data = [];
            data['childrens']    = Z_Editor.elements[id];
            data['parent']      = dir_name;
            var _options    = load_subcategories(data);
            jQuery( '.subcats' ).html(_options);
        }
    });

这是我尝试修改的CSS,但我只能将一个图像添加到背景中,这是静态的。我想添加3个应随文本更改的图像。 这是CSS

.tg-homebanner{position: relative;}
.tg-homebanner figure{
    width: 100%;
    float: left;
    margin: 0;
    z-index: 1;
    position: relative;
}
.tg-homebanner figure img{
    display: block;
    width: 100%;
    /*min-height: 650px;*/
}
.tg-banner-content{
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 2;
    background: rgba(0, 0, 0, 0.9);
}
/*Here I've made changes and successfully added a background image, but the problem is I want to put 3 images that should be changed with each click*/

.tg-banner-content .form-searchdoctors .tg-btn{color: #fff;}
.tg-homebanner .tg-searchform .tg-btn{color: #fff;}
.tg-homebanner .tg-location-map{height: 800px;}
.tg-mapbox .tg-location-map{
    height: 100%;
    position: absolute;
}
.show-search{
    position: absolute;
    height: 100%;
    width: 70px;
    right: 0;
    top: 0;
    background: rgba(0, 0, 0, 0.5);
    color: #fff;
    font-size: 24px;
    text-align: center;
    display: none;
    cursor: pointer;
}
.show-search i{
    position: relative;
    top: 50%;
}

以下是我所做出的改变:

.tg-banner-content{
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 2;
    background: url('URL OF MY IMAGE') center center no-repeat;
  background-size: cover;
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-image:linear-gradient(to bottom right,#002f4b,#dc20000);
    opacity: 1;
}

但它只会使单个背景图像随点击而变化。

2 个答案:

答案 0 :(得分:0)

这是一个简单的按钮单击事件,可以使用jScript更改背景图像。这是一个示例代码

    <script>
function functionName() {
    document.body.style.background = "#000000 url('img.png') no-repeat right top";
}
</script> 

答案 1 :(得分:0)

这里有完整的代码

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Tabs - Default functionality</title>
  <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#tabs" ).tabs();
    $( "#tabs_head li" ).on("click",function(index, element) {
        var tabid=$(this).attr("data-tab");
        var tabimag=$(this).attr("data-src");
        //alert(tabimag)
        $("#"+tabid).css('background-image','url(' + tabimag + ')')

    });
  } );
  </script>
</head>
<body>

<div id="tabs">
  <ul id="tabs_head">
    <li data-tab="tabs-1" data-src="http://www.mssinfotech.com/uploads/slider/8e5a16689b28b64d49b5178fa0bd78c3.jpg"><a href="#tabs-1">Nunc tincidunt</a></li>
    <li data-tab="tabs-2" data-src="http://www.mssinfotech.com/uploads/slider/ac1549a0ba88a7d802e83ddbce7622e6.jpg"><a href="#tabs-2">Proin dolor</a></li>
    <li data-tab="tabs-3" data-src="http://www.mssinfotech.com/uploads/slider/c6693db9f27305653009f21affcb261d.png"><a href="#tabs-3">Aenean lacinia</a></li>
  </ul>
  <div id="tabs-1">
    <p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.</p>
  </div>
  <div id="tabs-2">
    <p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p>
  </div>
  <div id="tabs-3">
    <p>Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi neque rutrum erat, eu congue orci lorem eget lorem. Vestibulum non ante. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce sodales. Quisque eu urna vel enim commodo pellentesque. Praesent eu risus hendrerit ligula tempus pretium. Curabitur lorem enim, pretium nec, feugiat nec, luctus a, lacus.</p>
    <p>Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit, magna ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum rutrum. Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p>
  </div>
</div>


</body>
</html>