简体   繁体   English

如何更改完整滑块的背景图像 - bootstrap

[英]How to change the background-image of the full slider - bootstrap

I'm using the full slider bootstrap and would like to keep all the features, but instead of putting all images in the Carousel, I wonder if it is possible to keep only one and only change the background-image.我正在使用完整的滑块引导程序并希望保留所有功能,但不是将所有图像都放在轮播中,我想知道是否可以只保留一个并且只更改背景图像。

<!-- Full Page Image Background Carousel Header -->
<header id="myCarousel" class="carousel slide">
    <!-- Indicators -->
    <ol class="carousel-indicators">
        <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
        <li data-target="#myCarousel" data-slide-to="1"></li>
        <li data-target="#myCarousel" data-slide-to="2"></li>
    </ol>

    <!-- Wrapper for Slides -->
    <div class="carousel-inner">
        <div class="item active">
            <!-- Set the first background image using inline CSS below. -->
            <div class="fill" style="background-image:url('http://placehold.it/1900x1080&text=Slide One');"></div>
            <div class="carousel-caption">
                <h2>Caption 1</h2>
            </div>
        </div>
        <div class="item">
            <!-- Set the second background image using inline CSS below. -->
            <div class="fill" style="background-image:url('http://placehold.it/1900x1080&text=Slide Two');"></div>
            <div class="carousel-caption">
                <h2>Caption 2</h2>
            </div>
        </div>
        <div class="item">
            <!-- Set the third background image using inline CSS below. -->
            <div class="fill" style="background-image:url('http://placehold.it/1900x1080&text=Slide Three');"></div>
            <div class="carousel-caption">
                <h2>Caption 3</h2>
            </div>
        </div>
    </div>

    <!-- Controls -->
    <a class="left carousel-control" href="#myCarousel" data-slide="prev">
        <span class="icon-prev"></span>
    </a>
    <a class="right carousel-control" href="#myCarousel" data-slide="next">
        <span class="icon-next"></span>
    </a>

</header>

Finnaly:最后:

<!-- Full Page Image Background Carousel Header -->
<header id="myCarousel" class="carousel slide">

    <!-- Wrapper for Slides -->
    <div class="carousel-inner">
        <div class="item active">
            <!-- Set the first background image using inline CSS below. -->
            <div class="fill" **style="background-image:url('http://placehold.it/1900x1080&text=Slide One');"**></div>
            <div class="carousel-caption">
                <h2>Caption 1</h2>
            </div>
        </div>
    </div>

    <!-- Controls -->
    <a class="left carousel-control" href="#myCarousel" data-slide="prev">
        <span class="icon-prev"></span>
    </a>
    <a class="right carousel-control" href="#myCarousel" data-slide="next">
        <span class="icon-next"></span>
    </a>

</header>

I don't know why you would want this function, but since you're asking, I'll give you a solution.我不知道你为什么想要这个功能,但既然你问了,我会给你一个解决方案。

With the .css() -selector in jQuery, you can target CSS and change it with jQuery.使用 jQuery 中的.css()选择器,您可以定位 CSS 并使用 jQuery 更改它。

You can create an array or variable, loop through it with .each() , store that information in another variable and then let jQuery change the background-image after some time or after an action.您可以创建一个数组或变量,使用.each()循环遍历它,将该信息存储在另一个变量中,然后让 jQuery 在一段时间或某个操作后更改background-image You can change the background by using the following jQuery:您可以使用以下 jQuery 更改背景:

$('.classname').css('background-image', 'variable');

Just as an example (you'll have to figure out yourself how to do it with the background-images, how to let jQuery display another one etc), I have set up this fiddle .举个例子(你必须自己弄清楚如何处理背景图像,如何让 jQuery 显示另一个等等),我已经设置了这个 fiddle In the fiddle, I have a variable, which I split on the |在小提琴中,我有一个变量,我在| and which I then loop through with .each() .然后我用.each()循环。 This variable ( chunk ) is later used to make some new divs with the respective chunk as a classname.这个变量( chunk )稍后用于创建一些新的 div,并将相应的chunk作为类名。

jQuery jQuery

var somestring = "img1|img2|img3",
    separated = somestring.split("|");

$.each(separated, function(index, chunk) {
    $('.test').append('<div class="' + chunk + '">' + chunk + '</div>');
    //This line is purely added to do something with the chunks
});

I didn't want to make the whole code of you, since this site is made to help each other.我不想制作你的全部代码,因为这个网站是为了互相帮助。 I gave you a pretty good push to the right direction, you now just have to figure out how to change the background-image -property on a click or other action.我给了你一个很好的推向正确的方向,你现在只需要弄清楚如何在点击或其他操作时更改background-image属性。

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

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