当文本可见时在滚动过程中更改图像

问题描述 投票:0回答:2

我有这个脚本,当text到达屏幕顶部时,它会更改图像。我要相反。当text从底部的某个高度(例如50像素)变为可见时,我希望更改图像。我该怎么办?

JS小提琴:https://jsfiddle.net/ramo427e/

HTML:

<div class="main">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <h1>This is sparta and what comes whatever after. This is sparta and what comes whatever after. This is sparta and what comes whatever after. </h1><h1>This is sparta and what comes whatever after. This is sparta and what comes whatever after. This is sparta and what comes whatever after. </h1>
                </div>
                <div class="col-md-6">
                    <section id="one">
                        <div class="content">
                            <h1>first heading</h1>
                            <p>description</p>
                        </div>
                    </section>
                    <section id="two">
                        <div class="content">
                            <h1>second heading</h1>
                            <p>description</p>
                        </div>
                    </section>
                    <section id="three">
                        <div class="content">
                            <h1>third heading</h1>
                            <p>description</p>
                        </div>
                    </section>
                    <section id="four">
                        <div class="content">
                            <h1>fourth heading</h1>
                            <p>description</p>
                        </div>
                    </section>
                </div>
                <div class="col-md-6">
                    <div class="image"></div>
                </div>
            </div>
            <div class="row">
                <div class="col-12">
                    <h1>This is sparta and what comes whatever after. This is sparta and what comes whatever after. This is sparta and what comes whatever after. </h1>
                </div>
            </div>
        </div>
    </div>

CSS:

*{
   margin:0;
   padding:0;
}
h1{
  font-size: 18px;
  font-weight: strong;
}
p{
  font-size: 12px;
}
.main{
    width:100%;
    height:auto;
    position: relative;
}
.image {
    background-image:url('https://i.postimg.cc/FRxNp6yG/hr-ax.png');
    background-size:100% 100%;
    background-attachment:fixed;
    transition: 1000ms;
    width: 200px;
    height: 200px;
    position: fixed;
}
#one, #two, #three, #four {
    min-height: 150px;
}

JS:

$(window).on("scroll touchmove", function() {
            if ($(document).scrollTop() >= $("#one").position().top && $(document).scrollTop() < $("#two").position().top) {
                $('.image').css('background-image', 'url(https://i.postimg.cc/FRxNp6yG/hr-ax.png)')
            };
            if ($(document).scrollTop() >= $("#two").position().top && $(document).scrollTop() < $("#three").position().top) {
                $('.image').css('background-image', 'url(https://i.postimg.cc/wvz9hzm7/hr-bx.png)')
            };
            if ($(document).scrollTop() >= $("#three").position().top && $(document).scrollTop() < $("#four").position().top) {
                $('.image').css('background-image', 'url(https://i.postimg.cc/FRxNp6yG/hr-ax.png)')
            };
            if ($(document).scrollTop() >= $("#four").position().top) {
                $('.image').css('background-image', 'url(https://i.postimg.cc/wvz9hzm7/hr-bx.png)')
            };
        });
javascript jquery scroll scrolltop
2个回答
0
投票
样品:

window.addEventListener('scroll', function() { var element = document.querySelector('#main-container'); var position = element.getBoundingClientRect(); // checking whether fully visible if(position.top >= 0 && position.bottom <= window.innerHeight) { console.log('Element is fully visible in screen'); } // checking for partial visibility if(position.top < window.innerHeight && position.bottom >= 0) { console.log('Element is partially visible in screen'); } });

更多:

https://usefulangle.com/post/113/javascript-detecting-element-visible-during-scroll

0
投票
if($(document).scrollTop() >= $("#one .content h1").position().top - 50){ $('.image').css('background-image', 'url(https://i.postimg.cc/FRxNp6yG/hr-ax.png)') }
© www.soinside.com 2019 - 2024. All rights reserved.