简体   繁体   English

在fullpage.js中反转图像回调的颜色

[英]Inverting colours of an image callback in fullpage.js

I've changed the slide arrows to my own png, black arrows but on some slides, which have darker background I want to invert them so they are white. 我已将幻灯片箭头更改为自己的png,黑色箭头,但在某些幻灯片上,背景颜色较深,我想将它们反转为白色。

-webkit-filter: invert(100%);

works just fine, but how to trigger this only on slides that using a callback? 效果很好,但是如何仅在使用回调的幻灯片上触发它?

Quick animation of the invert parameter 0% > 100% would be a cool bonus. 反转参数0%> 100%的快速动画效果会很不错。 Thanks. 谢谢。

I didn't know about fullPage.js , but reading through the docs , I found that you can customize a callback that fires when leaving or entering a slide. 我不了解fullPage.js ,但是通读文档后发现,您可以自定义在离开或进入幻灯片时触发的回调。

I think this is what you want: 我认为这是您想要的:

jsFiddle - https://jsfiddle.net/ym3snhu4/1/ jsFiddle - https: //jsfiddle.net/ym3snhu4/1/

Notice that I'm only using -webkit-filter to invert the color, so this might not work in other non-webkit browsers. 请注意,我仅使用-webkit-filter反转颜色,因此这可能在其他非webkit浏览器中不起作用。 So test this in Chrome, for instance. 因此,例如,在Chrome中进行测试。 Otherwise, just add more prefixes to the filter property when adding/removing the style. 否则,在添加/删除样式时,只需在filter属性中添加更多前缀即可。 It depends on how you are changing your arrows, but you can, for instance (with fullPage.js), just change the border-color instead of using filter. 这取决于您如何更改箭头,但是例如(使用fullPage.js),您可以更改边框颜色而不使用滤镜。

Basically, we are using the afterSlideLoad and the onSlideLeave callbacks to achieve this. 基本上,我们使用afterSlideLoadonSlideLeave回调来实现此目的。 Here's the code with the explanation in comments. 这是带有注释说明的代码。 It's pretty straightforward. 这很简单。 My comments are way longer than the code needed, just hoping it's clear. 我的注释比所需的代码长得多,只是希望它很清楚。

Example HTML: HTML示例:

<div class="section">
    <div class="slide" data-anchor="slide1"> Slide 1 - Index 0 </div>
    <div class="slide" data-anchor="slide2"> Slide 2 - Index 1 </div>
    <div class="slide slide-dark" data-anchor="slide3"> Slide 3 - Index 2 <br> I'm black </div>
    <div class="slide" data-anchor="slide4"> Slide 4 - Index 3</div>
</div>

JavaScript: JavaScript的:

// Just take into consideration the callback methods.
// The body selector is the one I used in the jsFiddle, keep whatever you have.

$(document).ready(function() {
    $('body').fullpage({

        // The name is self explanatory. 
        // Fires when a a slide has finished loading
        // and returns info about the slide.
        // This is helpful so we can know when we are on the
        // darker slide(s)
        afterSlideLoad: function (anchorLink, index, slideAnchor, slideIndex) { 

            // Notice that in the HTML I set the data-anchor attribute, 
            // since that can help us be specific about what slide we are in. 
            // Otherwise, just use the index, which is probably a good idea 
            // since onSlideLeave doesn't return the slideAnchor parameter, 
            // for some reason.
            // Change the if statement to whatever fits your needs.
            // Check what index and/or anchor your darker slides are.

            if(slideAnchor === 'slide3' || slideIndex === 2) {

                // This adds the invert filter to the controlArrow(s),
                // effectively reversing their color WHEN inside of
                // this specific slide.
                // (In this example, slideIndex 2 with anchor 'slide3').
                $('.fp-controlArrow').css('-webkit-filter', 'invert(100%)');
            }
        },

        // This fires when leaving a slide.
        // This will be helpful to return the arrows to their 
        // default color. (When we know we've inverted them beforehand)
        onSlideLeave: function (anchorLink, index, slideIndex, direction) {

            // We inverted the color of the arrows in slide 3.
            // When leaving this slide, we roll them back to their
            // original color, by setting the filter to 'none'.
            // I don't know why this 'event' doesn't return the
            // slideAnchor parameter, so we will just use the index
            // in here, which is slideIndex === 2 for 'slide3'.
            // Again, change the if logic to fit your needs, i.e. add
            // more slides.

            if(slideIndex === 2) {

                // This removes the filter, arrows go back to their
                // original color.

                $('.fp-controlArrow').css('-webkit-filter', 'none');

            }
        }
    });
});

And the CSS needed for the smooth transition on filter (change the animation speed to whatever you wish): 以及用于平滑过渡滤镜所需的CSS (将动画速度更改为所需的任何值):

.fp-controlArrow {
    -webkit-transition: all 500ms;
    transition: all 500ms;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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