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

The final item in the Bootstrap carousel will automatically conceal the right control option

Currently utilizing bootstrap 4.0, I am seeking a solution to hide the left control on the first item and hide the right control on the last item within the carousel. I believe jQuery can assist in achieving this. The left control should be hidden at all ...

Contact Form featuring a Text Area to complete the rest of the details

I am currently working on creating a contact form that has a design similar to the one shown below. However, I am facing challenges in getting the desired layout. I initially tried using flex-box but encountered issues with displaying the Text Area correct ...

Tips for handling a disabled button feature in Python Selenium automation

When trying to click this button: <button id="btn-login-5" type="button" class="m-1 btn btn-warning" disabled="">Update</button> I need to remove the disable attribute to make the button clickable. This ...

Steps on removing a file type from a Material UI textfield

I've been struggling to figure out how to clear a Material UI textfield with type="file" even after trying multiple approaches. My issue arises when I set a file size limit and display an error message if a user tries to upload a file larg ...

Facing delays in receiving responses for inner loop in node.js

Having an issue with retrieving data from the inner loop, as there is a delay in getting it. In my code, I am fetching the user list along with their ancestors from the SQL table. The ancestors are needed to verify their root values in the hierarchy/tree ...

Implementing file change detection using view model in Angular

When using the input type file to open a file and trigger a function on change, you can do it like this: <input type="file" multiple="multiple" class="fileUpload" onchange="angular.element(this).scope().fileOpened(this)" /> The functi ...

Solving the default font problem in Laravel

I've been working on a project using Laravel and Bootstrap 4. Recently, I decided to switch from the default Laravel font, Nunito, to Lato. So, I made the necessary changes in my app.css file: @import url(https://fonts.googleapis.com/css?family=Nun ...

The anchor tag fails to trigger the onClick function in React

I'm having trouble updating the component state in my React app when clicking on an anchor tag within the render method. I've attempted to bind the function in the constructor, but the console.log statement is still not being called. Here's ...

The NodeJS application experiences a crash if incorrect parameters are provided to the API

Currently, I have built a CRUD API using TypeScript with Node.js, Express, and MongoDB. My goal is to ensure that the API functions correctly when the correct parameters are sent through a POST request. However, if incorrect parameters are passed, the Node ...

Problems with script-driven dynamic player loading

I am attempting to load a dynamic player based on the browser being used, such as an ActiveX plugin for Internet Explorer using the object tag and VLC plugin for Firefox and Google Chrome using the embed tag. In order to achieve this, I have included a scr ...

What's the best way to align grid items that are positioned next to each other in CSS Grid?

I'm having trouble aligning two grid elements next to each other. What I want is for the heading, paragraph, and bottom links to be in one grid element and the image to be in another. When I tried using grid-template-columns: 1fr 2fr;, one element got ...

Discover the best way to sort products by category

I'm in the process of developing an online store where customers can choose from three options: "body lotion," "body wash," and "body scrub." Once a selection is made, the corresponding product will be displayed. The products are stored in an array n ...

Tips for emphasizing a searched item in V-data-table using VueJS

Is it possible to highlight a specific value in a v-data-table by searching for a particular word? Below is a snippet of my current code: EDIT: I am using the CRUD-Actions Example from the official Vuetify documentation. https://vuetifyjs.com/en/componen ...

Analyze and tally occurrences in two arrays

I have two arrays $array1=('18753933','18753933','18771982') $array2=('18753933','18771982') My goal is to compare the values that are the same in both arrays. var countArticlesLoaded=0; for(var $i=0;$i ...

Best Practices for Iterating over Nested Objects and Arrays Using ng-repeat

I've been trying to use Angular's ng-repeat, but nothing seems to work for me. I'm attempting to loop through companies.users and display all the first names. Any assistance would be greatly appreciated! Thank you! <div ng-app="app" ng-c ...

The JSON.stringify function was successfully sent to my PHP file, but upon attempting to decode it with json_decode, an error occurred which

Using jQuery to send data: jsonString = JSON.stringify(arrReturn); $.ajax({ url: 'add-performa-invoice.php', method: 'post', data: {'pro_table': jsonString}, success: function(data, status, j ...

What is the best way to transfer data from MongoDB (utilizing the Mongous module) to a Node.js view (using the Jade templating

Hello everyone, I have a quick question regarding passing data from a model (database) into a view. I am using Express, Mongous (not Mongoose) to access MongoDB, and Jade for my views. Despite trying to use Mongoose, I have not been successful in achieving ...

Customize Angular Material styles uniquely across various components

Within my application, I am utilizing two components that contain tab groups. The first component serves as the main page, where I have adjusted the CSS to enlarge the labels by using ViewEncapsulation.None. The second component is a dialog, and I aim to m ...

Wait for a reply from one GET request before initiating the next one in node

When working with node, I am making two consecutive calls to an API. My goal is to ensure that the first GET request has completed before triggering the second one, using data from the response of the first call. To achieve this, I have experimented with ...

Positioning a dropdown menu on top of a Google Map in React with JavaScript: Best Practices

I've been attempting to place a dropdown menu at a specific location on my Google Map, specifically in the top right (or top left is also acceptable). Below is the current output I am seeing: Here is the expected result: Modifications After trying ...