Blend pictures using jQuery

Hello, I am currently working on implementing a crossfade effect for the banner images on my homepage using jQuery. The fading effect is functioning correctly with the following code:

<script>
 function bannerImages(){
  var $active = $('.banner-test .banner_one');
  var $next = ($active.next().length > 0) ? $active.next() : 
 $('.banner-test img:first');
  $next.css('z-index',2);//move the next image up the pile
  $active.fadeOut(1500,function(){//fade out the top image
  $active.css('z-index',1).show().removeClass('active');//reset the z-index and unhide the image
      $next.css('z-index',3).addClass('active');//make the next image the top one
  });
}

  $(document).ready(function(){
    // run every 7s
   setInterval('cycleImages()', 7000);
  })
</script>

While this is working well, there is one issue that I have encountered. In order for the crossfade effect to work properly, position:absolute needs to be applied to the .banner-test img class. However, within the .banner-test class, there is another div used to display text on top of the banner image:

 <div class="banner-test">
        <img class="banner_one" src="../image.jpg" alt="" />
         <img src="../image2.jpg" alt=""/>

        <div id="text">
            <p class="text1">Sample Text</p>
        </div>
    </div>

This brings me to the css for the #text:

 #text {
position:absolute; 
bottom:35px ;  
left:10px;  
width:70% ;   
background-color:#104E8B;  
font-size:1em;  
color:white;  
opacity:0.95; 
filter:alpha(opacity=95); /* IE transparency */  
}
.text1 {
padding:13px;  
margin:0px;  
}
.banner-test {
  display: block;
  position: relative;
}

If I apply absolute positioning to the image, it disrupts the layout with the text as everything gets pushed to the top of the page. Does anyone have suggestions for a workaround?

UPDATE

https://jsfiddle.net/ztrez888/1/embedded/result/ - in this fiddle, applying position absolute to the .banner-test img causes the text to disappear.

Answer №1

You mentioned: (everything is aligned to the top of the page)

The reason for this issue is that your wrapper element .banner-test does not have a fixed height set. Therefore, when you apply absolute positioning to the images inside it, the .banner-test element shrinks to the height of the #text .text1.

To solve this, you can either set a specific height in the CSS:

.banner-test {
  display: block;
  position: relative;
  height:200px; /* <--enter the height of the image */
}

Alternatively, you can calculate the height using jQuery:

$(document).ready(function(){
    var arr = $('.banner-test img').map(function(){ // store the heights of images in an array
                  return $(this).height();
             }).get(),
       h   = Math.max.apply(Math, arr); // find the greatest height

    $('.banner-test').css('height', h); // set the height dynamically
    // image cycling every 7 seconds
    setInterval('cycleImages()', 7000);
});

cycleImages() is called within the setInterval and assumes the presence of the bannerImages() function on the page.


Updates:

  #text {
    position: absolute;
    bottom: 30px;
    left: 10px;
    width: 100px;
    background-color: #104E8B;
    font-size: 1em;
    color: white;
    opacity: 0.95;
    filter: alpha(opacity=95);
    /* IE transparency */
    z-index: 5;  /* <----modify these values accordingly*/
    left: 10%;
    top: 0;
  }

Updated fiddle link

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

What are some tactics for circumventing the single-page framework behavior of next.js?

How can I change the behavior of next.js to load each URL with a full reload instead of acting like a one-page framework? ...

Troubleshoot issues within ExpressJS

I am encountering an issue with the debugger in Visual Studio Code. It crashes upon startup, but runs smoothly when using nodemon to start the server. My application is connected to a MySql Database. I have attempted to reinstall the module without succes ...

Prevent the ReactJS submit button from being activated if the text area contains any missp

I've been working with ReactJS and recently designed a form. One of the requirements is to disable the send button if the text area in the form contains spelling errors. I attempted using a spell check feature, however, it only underlines the errors ...

What is the best method for transforming JSON into JSONP?

I have written the following code to send JSON data to the front end. However, we are facing cross-domain security issues and need to convert it to JSONP. Can anyone suggest what modifications I should make for this conversion? Server-side Code JsonFac ...

Encoding a two-dimensional array into JSON format

In my PHP script, I am querying a database and formatting the results as JSON: $query = mysql_query($sql); $rows = mysql_num_rows($query); $data['course_num']=$rows; $data['course_data'] = array(); while ($fetch = mysql_fetch_assoc($q ...

Troubleshooting a CSS problem within Visual Studio Code

Just started using VS Code and I'm having trouble getting my CSS to work. The link seems to be fine since it opens with click + ctrl, and both files are in the same folder. So why isn't it applying the styles? https://i.sstatic.net/yRysK.png Ed ...

Guide to generating an HTML table with database information employing jquery and ajax

I am faced with the challenge of creating a table using information retrieved from a database. Despite being able to view all the data through console.log, I am struggling to convert that into a table format. My latest version now displays at least one op ...

Achieving space between elements using Flexbox with a line separating each row

I have already found a solution to this question, but I am interested in exploring better alternatives. My goal is to utilize flexbox to create rows of items with equal spacing and dividing lines between them. The examples provided in the code snippet achi ...

Troubleshooting: Problems with Angular 2's [hidden] and *NgIf functionalities

My table displays a green circle if data is available in the gegevensAanwezig variable, which contains a school name, school code, and various booleans (Entreetoets, Eindtoets, etc) to indicate data availability. <tr *ngFor="let g of gegegevens ...

Webpack is ejected from watch mode

Currently, I am in the process of learning React and have decided to use webpack alongside it. However, I seem to be facing an issue with webpack when it comes to the watch mode. It builds the files once but then halts. The console output reflects the foll ...

Are there any alternative methods to refresh the Google Search Result for my website? (Meta tags are ineffective)

I'm facing an issue with the Google search result of my website where it's displaying old text instead of what I intended. I've tried using meta tags like nosnippet and description, but the problem persists. Can anyone suggest resources or ...

I encountered an issue with the timeouts() and windows() methods where I received an error message stating "The method window() is undefined for the type WebDriver.Options"

Currently, I am utilizing the driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS); and driver.manage().window().maximize(); methods in my code. However, encountering an error with the timeouts() and window() functions. The error message states ...

Tips for transforming user inputs in HTML format into text within a prompt box

Is there a way to create a text prompt box for the user to input their name without risking the insertion of HTML elements into the page? I want to avoid any potential issues like inadvertently creating a new HTML element instead of displaying the intended ...

changing a string into an array in react native

Extracted from an API, I am given the following string: [10, 20, 22, 26]. These values are mapped to [A, B, C, D], and I have a slider with values [1, 2, 3, 4]. What I am trying to achieve is that when you select 1 on the slider, it displays the value of A ...

Strategies for Concealing an Image When CSS is Active in Internet Explorer

My login functionality includes an input box with a CSS image that disappears onclick, allowing users to enter information. I used an image because the text password displays as "*", but this setup is not working in IE. Can anyone assist? HTML: <form ...

EJS: Dynamically linking CSS and JS files according to specific page conditions

Is there a way to conditionally call CSS/JS files based on specific page conditions in EJS? Can we use a flag from the router or base it on the URL in the EJS file? Note: The code below works perfectly when accessing the /editor page, but it will cause er ...

AXIOS: Is it possible to send multiple data items in a single POST request to an API?

My form collects data, which is then sent to an API using the new FormData(). However, one of the items in the model contains multiple attributes in the MongoDB model. Since the form requires a 1-to-1 key-pair relationship, it can't be sent as an obje ...

What is the best way to replace HttpClient in Aurelia?

I am completely new to Aurelia. How can I modify the code below in order to implement a dummy HttpClient, such as a json reader, that will provide a static set of json data without the need for a server during development? import {inject} from 'aure ...

Streamlined payment with PayPal Express Checkout via AJAX

I am working on integrating a JQuery client with PayPal's Express Checkout functionality for credit card transactions. The process involves the client presenting a form to the user, initiating a purchase via AJAX to the server, executing SetExpressChe ...

What is preventing me from altering the array one element at a time?

I am working with an array and a class let questions = [ { questionText: '', answerOptions: [], }, ]; class Questions { constructor(questionText,answerOptions) { this.questionText = questionText; this.answerOptio ...