What is the best way to programmatically rotate the background image of a div that employs background-size: cover using Jquery?

I've been attempting to change the background image on a website by rotating through an array of images, but unfortunately, I am encountering difficulties. Despite my efforts, the console doesn't provide any clues as to what might be going wrong. I have exhausted all my ideas and would greatly appreciate some assistance. The goal is to rotate the background images based on the array provided. It's unclear why it isn't working; could it be that the cover property of the background is causing issues? I have spent hours modifying the code, initially trying without populating the array with new Image() constructors, then making adjustments in hopes of resolving the issue. I referenced a solution from this website (link) for most of my code.

$(document).ready(function() {
fondos = new Array('/Foto_Pimend/activos/flickr_foto_fondo_1.jpg', '/Foto_Pimend/activos/flickr_foto_fondo_2.jpg', '/Foto_Pimend/activos/flickr_foto_fondo_3.jpg');
cargar_fondos = new Array();
fondo_num = 1;
var i;
for (i = 0; i < fondos.length; i++) {
cargar_fondos[i] = new Image();
cargar_fondos[i].src = fondos[i];
}
function rotarFondos() {
$('.fondo_intro').css({'background': 'url("' + cargar_fondos[fondo_num++ % cargar_fondos.length].src + '") no-repeat 50% 100% cover'}, 'slow');
}
intId = setTimeout(rotarFondos, 10000);
});

The div I wish to modify programmatically has the following style:

.fondo_intro {background: url(/Foto_Pimend/activos/flickr_foto_fondo_1.jpg) no-repeat; width:100%; background-size:cover; background-position:50% 100%;position:fixed; height:100%;}

I also attempted to link the function to a click event used to change content:

$('.link_manejador').click(function() {
var content = $(this).attr('name')+'_template';
llamada_contenido(content); rotarFondos();  
});

Instead of a rotating background, I aimed to have a dynamic background that changes based on content. Unfortunately, this approach didn’t yield results either, and the console remains silent—no errors or hints. I can see the images being created and the counter incrementing, yet nothing transpires. Even when I try changing the background manually via the console:

$('.fondo_intro').css({'background-image':'url(/Foto_Pimend/activos/flickr_foto_fondo_2.jpg'},'slow')

No changes occur. There seems to be a fundamental misunderstanding on my part.

Answer №1

Exclude "cover" in the following code:

$('.fondo_intro').css({'background':'url("'+cargar_fondos[fondo_num++ %cargar_fondos.length].src+'") no-repeat 50% 100% cover'},'slow');

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

Querying val results in an empty string being returned

In my application, I have the following HTML code snippet: <tr style="cursor:pointer;"> <td>100101</td> <td>Monkey</td> <td>Nuts<td> <td>Cow</td> <td>Soup</td> <t ...

What is the best way to update or include a new class in the jQuery mobile loader widget?

After reviewing the documentation at this link: I discovered a method to modify or add the default class of the loader widget, ui-loader. Despite my attempts, I have not been able to see any changes. You can view my demo here: https://jsfiddle.net/yusril ...

Overflowing content in Bootstrap div causing image gallery to surpass its boundaries

I am struggling with aligning my images properly in a responsive gallery using Bootstrap's grid system. Despite numerous attempts, the divs are consistently off-center to the left, and I can't seem to adjust their padding or center the images wit ...

Using CSS in React does not override the CSS preview in Ant Design for React

Working with Tables using antD has been quite an experience. By default, hovering over a Table Row in antD triggers a subtle gray color transition as a highlight. However, I wanted to switch things up and change the color to something different. My attempt ...

Tips on customizing the selected icon color in Material-UI's BottomNavigationAction styling

I'm facing an issue with Material-UI styled components and could use some assistance. My goal is to create a BottomNavigation bar in React using Material-UI v5 where the selected option's icon displays in red (#f00) while the unselected icons sho ...

Tips for adding a border to a 3D pie chart in Highcharts

I created a 3D pie chart using the Highchart plugin and wanted to add borders to each portion. I tried adding the following code: borderWidth: 4, borderColor: "red" within the plotOptions: pie: section. However, I noticed that the portions were getting b ...

Retrieve information using JSON.parse from an ajax request

I have been searching high and low for the solution. Here is the JSON string "[{"id":"0"}]" I attempted obj['id'] and obj.id but to no avail $.ajax({ url: 'php/checkdoctorappointmentonday.php', data: 'doc ...

Reordering elements with jQuery by dragging and dropping, then dragging them again for further

I am currently working on a drag and drop feature for a webpage where users can drag elements from a list into different droppable containers. The initial drag and drop functionality is working perfectly, but now I want to enable users to move these elemen ...

Sorting through JSON information using JQUERY

Struggling with filtering data using jQuery and JSON. I need to exclude objects with the ID "234" or the sender named "Alan Ford" in order to create separate messaging systems for Inbox and Outbox. Directly manipulating the JSON is not an option. Here&apo ...

What is the best way to adjust the size of an SWF object using a Blueprint CSS span range?

Utilizing Blueprint CSS, I am crafting a 3 column layout: LEFT COLUMN: span-6 CENTER COLUMN: span-12 RIGHT COLUMN: span-6 Within the center column, I aim to insert an SWF object that requires a predetermined size upon setup: swfobject.embedSWF(url, "fla ...

Retrieve data from controller using jQuery Ajax

I am in need of assistance with my HTML form and controller interaction. I require a Boolean result from the controller based on user input. Here is an overview of my current form: <div id="temp"></div> @using (Html.BeginForm("Re ...

After receiving a data token from the server in one controller, how can I efficiently utilize that token in a different AngularJS controller?

In my adminSearchCtrl controller, I am receiving data from the server in the form of a token and want to pass that token to another controller named "adminViewCtrl". How can I achieve this? adminSearchCtrl.js $scope.getUserDetails = function(selectedUser ...

Show information according to the chosen start and end dates on fullcalendar

I am currently using full calendar to display some content on a calendar. 1) By default, the calendar is showing data in all date fields. 2) I have added start and end date selectors at the top of the calendar. I want to only display data based on the se ...

Populate the dropdown menu with data from a JSON file

Recently, I created a custom JSON file and wanted to populate a select>option using this data. However, I encountered an error message saying: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:///C:/.../p ...

JavaScript backspace function not functioning correctly

I'm having issues with the Backspace keycode not working. I've tested it in IE and Google Chrome, but it's not displaying anything in the console or the alert. Here is the code snippet: $(document).keypress(function(e) { con ...

Creating a Duplicate of the Parent Element and its Child Elements using jQuery

Here is a code snippet I have that adds a new paragraph when a button is clicked. Currently, the script clones the "sub" div and appends it to the "main" div. However, the issue is that it only copies the content of the "inner" div within the "sub" div ins ...

What is the best way to send selected checkboxes from a form to a PHP page in the form of an array using a jQuery submit feature?

Currently, I am working on a project that involves a list of checkboxes for user selection. The checkboxes are dynamically generated from a mySQL query, allowing users to select multiple options. Here is the code snippet for generating the checkboxes: ech ...

The Yii yiiactiveform function remains undefined when clientValidation is disabled

I am currently using Yii 1.1.14 Instead of using Yii's ajax validation or client validation, I am using a form to capture an email. <?php $form = $this->beginWidget('CActiveForm', array( 'id' => 'form-newsletter ...

The data type "100%" cannot be assigned to a number type in the kendo-grid-column

I need the columns in the grid to have a width of 100%. <kendo-grid-column field="id" title="id" [width]="'100%'"> </kendo-grid-column> However, when I hover over the code, I get this message: The value "100%" cannot be ...

Expand the table in the initial state by using CSS to collapse the tree

I am struggling to collapse the tree view and need assistance. Below is the code snippet I've added. My goal is to have each node in the tree view initially collapsed and then expand a particular node on user interaction. For example, when I execute ...