在视口中将类添加到div [英] Add class to div when in viewport

查看:76
本文介绍了在视口中将类添加到div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当另一个div进入视口时,我想向菜单项添加一个类.

I want to add a class to a menu item when a different div enters the viewport.

我已经成功复制了以下警报: http://jsfiddle.net/blowsie/M2RzU/但是,当我尝试将其从警报更改为addClass或toggleClass时,它会中断.这是我的代码:

I have successfully replicated the alert found here: http://jsfiddle.net/blowsie/M2RzU/ However, when I try to change it from an alert to an addClass or toggleClass it breaks. This is my code:

$('#portfolio').bind('inview', function (event, visible) {
  if (visible == true) {
    // element is now visible in the viewport
    $( "#menu-item-portfolio" ).addClass( ".active-area" );
  } else {
    // element has gone out of viewport
  }
});`

我想在#portfolio进入视口时将.active-area添加到#menu-item-portfolio中,然后在#portfolio离开视口时将其删除.

I want to add .active-area to #menu-item-portfolio when #portfolio enters the viewport, then remove it when #portfolio leaves the viewport.

推荐答案

我找到了解决方案.代码中有一个错字.代替 .addClass(.active-area") 它应该是 .addClass(活动区域")

I found the solution. There was a typo in the code. Instead of .addClass(".active-area") it should be .addClass("active-area")

这篇关于在视口中将类添加到div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆