简体   繁体   English

当元素在视口中可见时,对div进行动画处理

[英]Animate a div when an element is visible in the viewport

Im am trying to create a horizontally scrolling, one-page website where I am using divs as buttons to get the page to scroll to an element when they are clicked. 我正在尝试创建一个水平滚动的单页网站,我在其中使用div作为按钮来使页面在单击时滚动到某个元素。 I have the scrolling mechanism working but I want the divs to animate (change size or colour ect...) when the element they are linked to to is visible in the viewport. 我有滚动机制,但是当它们链接到的元素在视口中可见时,我希望div进行动画处理(更改大小或颜色等)。 What is the best way to do this? 做这个的最好方式是什么?

I'm not sure how you code is done for the scrolling because you didn't provide any code / exemple but I will try to help you in some way. 我不确定您的编码方式是如何完成的,因为您没有提供任何代码/示例,但我会尝试以某种方式为您提供帮助。

$(function(){
var sections = {},
    _height  = $(window).height(),
    i        = 0;

// Grab positions of our sections 
$('.section').each(function(){
    //this.name would be the attr name="" of a section but you could change to the id
    sections[this.name] = $(this).offset().top;
});

$(document).scroll(function(){
    var pos = $(this).scrollTop();

    // Look in the sections object and see if any section is viewable on the screen. 
    // If two are viewable, the lower one will be the active one. 
    for(i in sections){
        if(sections[i] > pos && sections[i] < pos + _height){
            $('a').removeClass('active');
            $('#nav_' + i).addClass('active');
        }  
    }
});

}); });

This Code should do what you want to achive atm it's just active a 'active' class but you could do a Css3 animation as you want. 该代码应该实现您希望达到的atm,它只是激活“ active”类,但是您可以根据需要创建Css3动画。

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

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