簡體   English   中英

背景圖片不會改變

[英]Background image won't change

我想在setInvterval中更改div的背景圖像,但不知道如何。 這是代碼:

$('.thumbnail').on({
        'mouseenter': function(){
            var thumbs = $(this).attr('data-num-thumbs');
            var thumb_url = $(this).css('background-image');
            console.log(thumb_url);
            var i = 1;
            setInterval(function(){
                var new_thumb_url = thumb_url.replace(/\d{1,2}\.jpg/gi, i + '.jpg');
                console.log(new_thumb_url);
                $(this).css('background-image', new_thumb_url);
                i++;
                if(i > 10) i = 1;
            }, 3000);
        });
});

這是div樣子:

<div data-num-thumbs="17" class="thumbnail" style="background: url('http://site/img/11.jpg') no-repeat;"></div>

看起來setInterval() $(this)映射到其他東西,試試這個

$('.thumbnail').on({
    'mouseenter': function(){
        $this=$(this);  //store $(this) in a local var
        var thumbs = $(this).attr('data-num-thumbs');
        var thumb_url = $(this).css('background-image');
        console.log(thumb_url);
        var i = 1;
        setInterval(function(){
            var new_thumb_url = thumb_url.replace(/\d{1,2}\.jpg/gi, i + '.jpg');
            console.log(new_thumb_url);
            //$(this).css('background-image', new_thumb_url);
            $this.css('background-image', new_thumb_url);
            i++;
            if(i > 10) i = 1;
        }, 3000);
    }
});

快樂編碼:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM