kennethcachia / background-check

Automatically switch to a darker or a lighter version of an element depending on the brightness of images behind it.
http://kennethcachia.com/background-check/
MIT License
3.27k stars 282 forks source link

Refresh only after window resize / iDangerous Swiper #35

Closed ough- closed 10 years ago

ough- commented 10 years ago

Hello, I got this working as far as I could on this test page: http://julia.samambaia.co.uk/3

But the text colour only changes after a bit of window resize. If you do that on the full screen slide you'll notice.

(disclaimer: I'm a graphic designer learning to code so please excuse my ignorance)

Thank you.

kennethcachia commented 10 years ago

Hi!

I can't access the link anymore but I've noticed that you're using BackgroundCheck on http://julia.samambaia.co.uk/. The logo and nav links change color from slide to slide. Are you still experiencing this issue?

(No problem!)

ough- commented 10 years ago

Hi, I managed to fix it but I suspect it's not very clean. I basically called the functions again after each click/swipe/scroll:

$(document).ready(function() { window.addEventListener('DOMContentLoaded', function () { // Prepare BackgroundCheck BackgroundCheck.init({ targets: '.target', images: '.change' }); });

$(document).on('click', function(e){ setTimeout(function(){ BackgroundCheck.refresh(); },300); });

$(document).on('mousewheel', function(e){ setTimeout(function(){ BackgroundCheck.refresh(); },300); });

$(document).on('keydown', function(e){ setTimeout(function(){ BackgroundCheck.refresh(); },300); });

$(document).on('touchmove', function(e){ setTimeout(function(){ BackgroundCheck.refresh(); },300); });

});

kennethcachia commented 10 years ago

Makes sense.

init() is used to initialize the plugin and process the targets and images once. In addition, you need to call refresh() every time the position of the targets and/or images change.

You have two options when using sliders:

Swiper has an onSlideChangeEnd callback function - http://www.idangero.us/sliders/swiper/api.php.

Try the following:

Exclude javascripts/text-colour-fs.js from your site and rewrite your existing block:

var mySwiper = new Swiper('.swiper-container')
$('.arrow-right').on('click', function(e){
  e.preventDefault()
  mySwiper.swipeNext()
})

to:

$(document).ready(function() {
  // Initialize Swiper
  var mySwiper = new Swiper('.swiper-container', {
    onSlideChangeEnd: function() {
      BackgroundCheck.refresh();
    }
  });

  // Prepare BackgroundCheck
  BackgroundCheck.init({
    targets: '.target',
    images: '.change'
  });

  $('.arrow-right').on('click', function(e) {
    e.preventDefault();
    mySwiper.swipeNext();
  });
});
ough- commented 10 years ago

That worked a treat, thanks!

On a slightly different note, is it possible to have the script working on both background-image and background-color?

Thanks again

kennethcachia commented 10 years ago

Awesome!!

At the moment, the plugin only supports images - using either <img> or CSS background-image.