简体   繁体   中英

How can I pause carousel/slider when I scroll from top and start again when I scroll to top?

I would like to achieve effect as on page: http://www.jovaconstruction.com/

I accomplished "blurring" effect, as my client wanted, that opacity of blurred image changes with scrolling, but I can not achieve that carousel/slider would pause. I tried several methods, but none of them was even close to solution.

Quick description of site: I have slider/carousel made as background over two sections and it should have effect as on example page.

My code:

html

<div class="slider">
        <div id="bg1" class="background activeslide"><div id="bg1blur" class="bluredbg"></div></div>
        <div id="bg2" class="background"><div id="bg2blur" class="bluredbg"></div></div>
        <div id="bg3" class="background"><div id="bg3blur" class="bluredbg"></div></div>
        <div id="bg4" class="background"><div id="bg4blur" class="bluredbg"></div></div>
    </div>

css

.background{
opacity:0;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
#bg1{
background: url('../img/bg1.jpg');
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
position:fixed;
width:100%;
z-index:-10;
height:100%;
}
#bg2{
background: url('../img/bg2.jpg');
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
position:fixed;
width:100%;
z-index:-10;
height:100%;
}
#bg3{
background: url('../img/bg3.jpg');
position:fixed;
width:100%;
z-index:-10;
height:100%;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
#bg4{
background: url('../img/bg4.jpg');
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
position:fixed;
width:100%;
z-index:-10;
height:100%;
}
#bg1blur{
background: url('../img/bg1blur.jpg');
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
position:fixed;
width:100%;
z-index:-10;
height:100%;
}
#bg2blur{
background: url('../img/bg2blur.jpg');
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
position:fixed;
width:100%;
z-index:-10;
height:100%;
}
#bg3blur{
background: url('../img/bg3blur.jpg');
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
position:fixed;
width:100%;
z-index:-10;
height:100%;
}
#bg4blur{
background: url('../img/bg4blur.jpg');
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
position:fixed;
width:100%;
z-index:-10;
height:100%;
}
.activeslide{
opacity:1 !important;
}
.bluredbg{
opacity:0;
}

js-slider

var displayTime = 5000;                                 //Time between    animations
var currIdx = 0;                                        //Index for loops
var $slides = $('.slider .background');                         //Html elements to cycle between

function animateBG() {
currIdx = (currIdx < 3) ? currIdx + 1 : 0;                              //We  set counter
setTimeout(function() {                                                     //Timeout for repetition
    $slides.removeClass('activeslide');                                 //We   remove class .activeslide from last element
    $slides.eq(currIdx).addClass('activeslide').fadeIn(0, function() {  //We add class to new element. 0 is trans. time(in this case css doc is doing this)
        animateBG();
    });
}, displayTime)                                                         //Displaytime is var = here we set last of animation of displaying 1 slide

}

js -opacity

//script for chng.opacitynclass .activeslide while scrolling down.
$(window).on('scroll', function () {
    var pixs = $(document).scrollTop()
    pixsiv = pixs / 500;
    $(".activeslide>.bluredbg").css({"opacity":+pixsiv})
});

Hopefully everything is clear. What is done and what I want.

Thank you.

How about calculating the offset for the window? :

window.scrollBy(100, 100);

if (window.pageXOffset !== undefined) { // All browsers, except IE9 and earlier
    alert(window.pageXOffset + window.pageYOffset);
} else { // IE9 and earlier
    alert(document.documentElement.scrollLeft + document.documentElement.scrollTop);
}

Eric thank you for your help.

My solution to my problem was a bit different.

I took bootstrap and jquery functionalities.

1st I rebuild my carousel to bootstrap standard. 2nd I used jquery scrollTop() function:

$document.scroll(function() {
  if ($document.scrollTop() > 5) {
    $('.carousel').carousel('pause');
  } else {
    $('.carousel').carousel();
  }
});

Solution is exactly what I wanted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM