Problem with swiper slider

Hi, is it possible to change the background-color of the text of a slide while I move the mouse to the second one?
What I try to do is to use “linear-gradient”, below is the code I try to use:

$(‘.swiper-slide’).on(‘mouseenter’, function(e) {
var parentOffset = $(this).parent().offset();
var relX = e.pageX - parentOffset.left;
var perc = relX / $(this).width() * 100;
$(‘#title’).css(‘background’, ‘linear-gradient(to right, #005487 ‘+perc+’%, #ffffff ‘+perc+’%)’);
});

unfortunately I can’t change the color when I try to go back to the first slide, can anyone help me?

Thank you.

Hi @ilariotresoldi,

Welcome to the forum.

Do you have a more complete example of your code? A link or maybe an example you could put on codepen or similar?

I know it’s probably clear in your mind what you are trying to achieve, but a more fleshed out example would help others with assisting you.

Hello, this is my code:

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/mobile/1.5.0-alpha.1/jquery.mobile-1.5.0-alpha.1.min.js"></script>

<script src=" https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js "></script>
<link href=" https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css " rel="stylesheet">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.css"/>
<script src="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.js"></script>
<style>
.swiper-slide { width: 88% !important; }

.bg {
  background: linear-gradient(to right, #005487 0%, #005487 33%, #fff 33%, #fff 100%);
  z-index: 1;
}

#title {
  /*background: linear-gradient(to right, #005487 0%, #005487 33%, #fff 33%, #fff 100%);*/
  /*
  -webkit-background-clip: text;
  -webkit-text-fill-color: #005487;
  */
}

.gradient-text {
  background: -webkit-linear-gradient(45deg, #ffffff, #005487);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
</style>
</head>
<body>

<div id="blu" style="width: 696px; height: 768px; position: absolute; left: 0px; z-index: 1;">
  <img src="bg005487.png">
</div>
<div class="row box-slider-discover" style="width: 100%; height: 768px; position: absolute; left: 0px; top: 0px; z-index: 2;">
  <div class="slider-discover-commands">
    <div class="slider-discover-prev swiper-button-prev"></div>
    <div class="slider-discover-next swiper-button-next"></div>
  </div>
  <div class="col-slider">
    <div class="swiper slider-discover swiper-initialized swiper-horizontal swiper-pointer-events">
      <div class="swiper-wrapper" style="transform: translate3d(0px, 0px, 0px);">

        <div class="swiper-slide swiper-slide-active" style="height: 768px;">
          <div class="row">
            <div class="col-lg-8" style="position: relative; left: 290px; top: 80px;">
              <h4 id="title" style="font: normal normal 900 120px/80px Montserrat; color: #005;">Individual &<br>Family</h4>
            </div>
          </div>
          <div class="row">
            <div class="col-lg-4" style="position: relative; left: 38px; top: 180px;">
              <p style="font: normal normal normal 20px/30px Montserrat; color: #ffffff; position: relative; left: 52px;">
              For <b>individuals and families</b>, we have<br>devised <b>wealth management and<br>planning strategies, investment<br>management</b> dedicated approaches,<br>and a bespoke <b>advisory & client care</b><br>section catering to every need.
              </p>
            </div>
            <div class="col-lg-4" style="position: relative; left: 86px; top: 180px;">
              <img src="images/Agave-Advisor-whealt-management.png" style="width: 504px; height: 234px;">
              <span style="font: normal normal 300 120px/80px Montserrat; position: relative; left: -456px; color: #ffffff; top: 40px; display: inline-block; width: 28px; height: 28px;">+</span>
              <span style="font: normal normal 600 20px/30px Montserrat; width: 295px; height: 24px; margin: 0 auto; color: #ffffff; display: inline-block; position: relative; top: -130px; left: 28%;">WEALTH MANAGEMENT</span>
            </div>
          </div>
          <div class="row">
            <div class="col-lg-4" style="position: relative; left: 616px; top: 250px;">
              <span style="font: normal normal 300 120px/80px Montserrat; position: relative; left: 89px; color: #005; top: -100px; display: inline-block; width: 28px; height: 28px;">+</span>
              <span style="font: normal normal 600 20px/30px Montserrat; width: 295px; height: 24px; margin: 0 auto; color: #005; display: inline-block; position: relative; top: -130px; left: 28%;">WEALTH PLANNING</span>
            </div>
          </div>
          <div class="row">
            <div class="col-lg-4" style="position: relative; left: 616px; top: 250px;">
              <span style="font: normal normal 300 120px/80px Montserrat; position: relative; left: 89px; color: #005; top: -100px; display: inline-block; width: 28px; height: 28px;">+</span>
              <span style="font: normal normal 600 20px/30px Montserrat; width: 295px; height: 24px; margin: 0 auto; color: #005; display: inline-block; position: relative; top: -130px; left: 28%;">CLIENT CARE</span>
            </div>
          </div>
        </div>
        <div class="swiper-slide swiper-slide-next" style="height: 768px;">
          <div class="row">
            <div class="col-lg-8" style="position: relative; left: 35px; top: 80px;">
              <h4 style="font: normal normal 900 120px/80px Montserrat; color: #005;">Open<br>Platform</h4>
            </div>
          </div>
          <div class="row">
            <div class="col-lg-4" style="position: relative; left: 38px; top: 180px;">
              <p style="font: normal normal normal 20px/30px Montserrat;"></p>
            </div>
            <div class="col-lg-4" style="position: relative; left: 86px; top: 180px;">
              <img src="images/Agave-advisor-Open  platform.png" style="width: 504px; height: 234px;">
              <span style="font: normal normal 300 120px/80px Montserrat; position: relative; left: -456px; color: #ffffff; top: 40px; display: inline-block; width: 28px; height: 28px;">+</span>
              <span style="font: normal normal 600 20px/30px Montserrat; width: 295px; height: 24px; margin: 0 auto; color: #ffffff; display: inline-block; position: relative; top: -130px; left: 28%;">PARTENERED INDIPENDENCE</span></div>
          </div>
          <div class="row">
            <div class="col-lg-4" style="position: relative; left: 616px; top: 250px;">
              <span style="font: normal normal 300 120px/80px Montserrat; position: relative; left: 89px; color: #005; top: -100px; display: inline-block; width: 28px; height: 28px;">+</span>
              <span style="font: normal normal 600 20px/30px Montserrat; width: 295px; height: 24px; margin: 0 auto; color: #005; display: inline-block; position: relative; top: -130px; left: 28%;">SUPPORT ECOSYSTEM</span>
            </div>
          </div>
        </div>

      </div>
    </div>
  </div>
</div>
<script>
const swiper_discover = new Swiper('.slider-discover', {
  	pagination: {
	    el: '.swiper-pagination',
	  },
  	navigation: {
	    nextEl: '.swiper-button-next',
	    prevEl: '.swiper-button-prev' 
	},
});
</script>

</body>
</html>

The problem is from how the mouseenter event interacts with the Swiper component, and to solve the problem you need to attach a mousemove event to the Swiper container rather than individual slides and then determine the slide under the mouse to calculate the gradient effect.
Here’s how you can do it:

$('.swiper-container').on('mousemove', function(e) {
    // Calculate the relative mouse position within the container
    var relX = e.pageX - $(this).offset().left;
    var perc = relX / $(this).width() * 100;
    
    // Determine which slide the mouse is over
    var slideWidth = $('.swiper-slide').width();
    var slideIndex = Math.floor(relX / slideWidth);
    var currentSlide = $(this).find('.swiper-slide').eq(slideIndex);
    
    if(currentSlide.hasClass('swiper-slide-active')) {
        $('#title').css('background', 'linear-gradient(to right, #005487 '+perc+'%, #ffffff '+perc+'%)');
    } else {
        $('#title').css('background', '');
    }
});

$('.swiper-container').on('mouseleave', function() {
    $('#title').css('background', '');
});

The code binds the mousemove event to the .swiper-container , calculates the percentage of the mouse’s position relative to the container width, and determines which slide the mouse is currently hovering over. If the mouse is hovering over the active slide, it applies the gradient effect. If the mouse leaves the container, it resets the background of #title .

Good luck

Hi, thanks for your help, unfortunately it’s not what I need, my fault for explaining myself badly, I’ll try to be clearer: I would need a JS code that when dragging the slide from the white background it passes over the blue background you change the portion of text with another color, which is why I had hypothesized the use of linear-gradient, but I have difficulty calculating the percentage of text to change based on the position on the container.

I think we’ll need to see a working copy as I copied the code you posted and I don’t see the blue background so its hard to see what effect you are trying to achieve. A lot of the html doesn’t make sense in a responsive environment either so there seems to be something missing or you have copied some live html rather than source.

If you could get a working copy in codepen (like this half working code of yours that I have just pasted) then I’m sure one of the JS gurus here can point you in the right direction.

However it sounds to me as though you are trying to change the color of some ‘partial text’ as it travels over a coloured background and that would be difficult to achieve other than using mix-blend mode as in this recent demo of mine.

1 Like

this is the problem, I have a div with a blue background and above a div with some text, this div must have two different colors as the slide moves:

The background-clip: text property is not fully supported in all browsers. You may need to consider a fallback for browsers that do not support these properties. However the browser support for them, This code will change the background of the text to your linear gradient when you hover over a .swiper-slide element, and then change it back when you stop hovering.

$(document).ready(function(){
  $('.swiper-slide').hover(
    function(){ // Mouse over
      $('#title').css('background', 'linear-gradient(to right, #005487 0%, #005487 33%, #fff 33%, #fff 100%)');
      $('#title').css('-webkit-background-clip', 'text');
      $('#title').css('-webkit-text-fill-color', 'transparent');
    },
    function(){ // Mouse out
      $('#title').css('background', '');
      $('#title').css('-webkit-background-clip', '');
      $('#title').css('-webkit-text-fill-color', '');
    }
  );
});

The above coding is just an example, hope this could help.

With mix blend mode you can have the color change but unfortunately you cannot specify the colors so all you get is the difference.This turns the white text black but keeps the blue text on the right

Assuming that was the effect you were going for anyway :slight_smile: I can’t see any other way to change it and I don’t see that adding a background when you move it is going to do anything as the text is already cut off on the static view.

Thanks to all for our support :slight_smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.