Issues arise when attempting to alter the background image using jQuery without browserSync being activated

I am facing an issue with my slider that uses background-images and BrowserSync. The slider works fine when BrowserSync is running, but without it, the animations work properly but the .slide background image does not appear at all. Can someone help me identify what I might be doing wrong? P.S. I also utilize a backgroundPosition script that allows for setting multiple arguments.

  1. See correct example: https://gyazo.com/2185ae332bac4b1da1bd4f2fcd9bfe5a

  2. See incorrect example: https://gyazo.com/681a28e927ef537f1f4f034faeb56b0d

html:

    <!DOCTYPE html>
<html>
<head>
    <title>Slider</title>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="styles/reset.css">
    <link rel="stylesheet" type="text/css" href="styles/main.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="js/backgroundPosition.js"></script>
    <script src="js/slider.js"></script>
    <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet"> 
</head>
<body>
    <div class="slider">
        <div class="upside">
            <div class="slide">
                <div class="wheel"></div>
            </div>
        </div>
    </div>
    <div class="next"></div>
</body>
</html>

css:

body{
    height: 100%;
    width: 100%;
}
.slider, .upside, .slide{
    height: 650px;
    width: 1200px;
    margin: 0 auto;
    border-radius: 15px;
}
.slider{
    margin: 50px auto;
    background: url(../img/bg.jpg) no-repeat center;
    overflow: hidden;
}
.upside{
    background-image: url(../img/upside.png);
    background-repeat: no-repeat;
    background-position: 0px -800px;
}
.slide{
    background-image: url(../img/sl_1.jpg);
    background-repeat: no-repeat;
}
@keyframes rt{
    100%{
        transform: rotate(360deg);
    }
}
.wheel{
    height: 640px;
    width: 180px;
    background-image: url(../img/wheel.png);
    background-repeat: no-repeat;
    background-position: left center;
    position: relative;
    margin: 0px 0 0 -180px;
    animation: rt 6s linear infinite
}
.next{  
    height: 120px;
    width: 120px;
    margin: 0px auto;
    font-family: 'Open Sans', sans-serif;
    color: white;
    font-size: 30px;
    text-align: center;
    line-height: 120px;
    vertical-align: middle;
    text-transform: uppercase;
    background:url(../img/next.png) no-repeat;
    border-radius: 50%;
    opacity: .55;
}

js:

$(function(){
    var btn = $(".next"),
        wheel = $(".wheel"),
        upside = $(".upside"),
        slide = $(".slide"),
        cur_slide = 1;
    var nextSlide = function(){
        btn.off();
        cur_slide++;
        if(cur_slide===4){cur_slide=1;}
        var i1 = 0;//upside bottom point
        var i2 = 650;//slide bottom point
        wheel.animate({
            margin: "0px 0 0 -105px"
        },800,
            function(){
                slide.animate({
                    backgroundPosition:("-1200 0")
                },1000,
                    function(){
                        upside.animate({
                            backgroundPosition:("0 0")
                        },1200,
                            function(){
                                slide.css('background-image',"url(../img/sl_"+cur_slide+".jpg)");
                                var up = setInterval(function(){
                                    upside.css("background-position","0 "+i1+"px");
                                    slide.css("background-position","0 "+i2+"px");
                                    if (i2===0) {
                                        clearInterval(up);
                                        upside.css("background-position","0 -800px");
                                        wheel.animate({
                                            margin: "0px 0 0 -180px"
                                        },800,
                                            function(){
                                                btn.click(nextSlide);
                                            })
                                    }
                                    i1--;
                                    i2--;
                                },2);
                            })          
                    })
            })
    }
    btn.click(nextSlide);
})

Here is the content of backgroundPosition.js (let me know if you need this or not)

(function($) {
 $.extend($.fx.step,{
 backgroundPosition: function(fx) {
 if (fx.pos === 0 && typeof fx.end == 'string') {
 var start = $.css(fx.elem,'backgroundPosition');
 start = toArray(start);
 fx.start = [start[0],start[2]];
 var end = toArray(fx.end);
 fx.end = [end[0],end[2]];
 fx.unit = [end[1],end[3]];
 }
 var nowPosX = [];
 nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
 nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
 fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

function toArray(strg){
 strg = strg.replace(/left|top/g,'0px');
 strg = strg.replace(/right|bottom/g,'100%');
 strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
 var res = strg.match(/(-?[0-9\.]+)(px|%|em|pt)\s(-?[0-9\.]+)(px|%|em|pt)/);
 return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
 }
 }
 });
})(jQuery);

Answer №1

In case anyone encounters a similar issue, I wanted to share the solution that worked for me:

slide.css('background-image',"url(../img/sl_"+cur_slide+".jpg)");

Simply update it to:

slide.css('background-image',"url(img/sl_"+cur_slide+".jpg)");

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Iterate through all elements in Javascript/jQuery while excluding any nested children elements

Looking to retrieve all elements in the document: $("*").each(function(){ var el = $(this); }); I want to target only parent elements, excluding their children. For example: <div> <!--TARGET--> <div></div> <!--IGNORE--&g ...

What is the reason for the return of undefined with getElementsByClassName() in puppeteer?

Currently, I am utilizing puppeteer to fetch certain elements from a webpage, specifically class items (divs). Although I understand that getElementsByClassName returns a list that needs to be looped through, the function always returns undefined for me, e ...

Implementing authorization middleware using Express.js in Ajax

My app has a straightforward authorization middleware that functions flawlessly with regular GET and POST requests. However, when I send a POST request via AJAX, the middleware fails to redirect to a 401 page and instead bypasses it, allowing the data to b ...

What is the benefit of utilizing pseudo elements to divide block elements?

I'm seeking clarification on a w3school example that showcases responsive grid design. <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> * { box-sizing: bo ...

Change the parent title attribute back to its original state

This is in contrast to queries similar to the one referenced here. In my scenario, there is a child element within a parent element (specifically a matSelect within a matCard, although that detail may not be significant) where the parent element has a set ...

Using Vue.js to loop through data objects when a button is clicked

I'm currently working on building a quiz functionality using vue.js, but I've hit a roadblock in trying to make the "Next" button cycle through my data. The goal is to have the screen display the object containing the question and answers (e.g. q ...

yet another dilemma with CSS height set to 100%

UPDATE: I believe there was an excess of information. Let me simplify my question: How can I adjust the height of #middle to be 100% of the remaining space in the example below: <body> <div id="big"> <div id="header"></div ...

Ways to eliminate unused classes from JQuery UI

We have successfully implemented JQuery UI library for enhancing our interface. However, since we no longer need to support outdated browsers like IE6, classes such as .ui-corner-all, .ui-corner-top, .ui-corner-left, .ui-corner-tl are unnecessary and clu ...

Incorporating a Drawer and AppBar using Material-UI and React: Dealing with the error message "Unable to access property 'open' of null."

Currently, I am working on developing an app using React and Material-UI. I have successfully implemented the AppBar component, but I am facing difficulties with setting up the Drawer functionality. The main issue I am encountering is an error message sta ...

Having trouble retrieving data from the PokeAPI

Currently, I am experimenting with PokeAPI for educational purposes and attempting to run the following code: const express = require('express') const https = require('https') const app = express() const port = 3000 app.get('/&apo ...

Retrieve item from sessionStorage Cart

Greetings to the Stack Overflow community! I've often relied on Slack for valuable information and found solutions to many of my questions. However, this time, I'm facing a challenge that has me stumped. While I'm not an expert in JavaScript ...

Slide your finger upwards to shut down the mobile navbar in bootstrap

Hey there, I'm currently developing a website using Bootstrap framework v3.3.4. I have a mobile navbar that opens when I click a toggle button, but I would like it to slide up to close the navigation instead. Here is an image showing the issue Do y ...

script to pause video using jQuery

I have a script that works perfectly, but currently it only stops an instance of a playing video if a new video is started. I want to modify the script so that ALL instances of playing videos are stopped when the function stopAllButMe(); is called. Can s ...

The most effective method for incorporating a header and footer into an HTML page utilizing a variety of client-side frameworks

Looking for a solution for my HTML project where I want to incorporate a header and footer to minimize rework. Any suggestions on a good approach? I have experimented with AngularJS using ng-include, and here is the code snippet: var app = angular.module ...

Error message in Angular js: Unable to load file using XMLHttpRequest

Encountering an error while debugging an Angular JS application in the WebStorm IDE. The error message states: "XMLHttpRequest cannot load file:///C:/Users/../list.html. Cross origin requests are only supported for HTTP." Provided Javascript code : var a ...

Using HTML, jQuery, and JSON with an AJAX call to populate two web tables stacked on top of each other

I am encountering an issue with populating two tables using two searches based on user input in mySQL-JSON-AJAX. When the user enters a search term and clicks the corresponding button, data should populate the respective table. The problem arises when clic ...

What methods can be utilized to ensure that my wistia video player/playlist is responsive on different devices

I need some assistance with making my Wistia player responsive while using a playlist. I want the video player to adjust its size based on the screen size. Here is an example: My current code utilizes their iframe implementation: <div id="wistia_1n649 ...

A combination of various select menus along with conditional statements

I am dealing with a situation where I have two select menus that can modify the displayed data on the page. Currently, I have code in place that updates the data based on changes made to one of the select menus, and it is functioning properly. This funct ...

Disable the javascript link on small devices

I currently have a javascript link (Trustwave) on my desktop website theme that I need to deactivate when viewed on mobile devices: Located in footer.tpl: <div style="position:absolute; margin-left:100px; margin-top:50px"> <script type="text/j ...

The spacing in the Superfish menu is not aligned correctly

I am encountering a spacing issue with a superfish drop down menu. The problem can be observed here: To view the issue in action, visit this link: Essentially, all of the submenu items are floated left with a 50% width. I am looking for a solution to rem ...