首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检查元素在视角2上的位置?

如何检查元素在视角2上的位置?
EN

Stack Overflow用户
提问于 2017-05-03 08:08:52
回答 3查看 6.6K关注 0票数 1

如何检查元素在视角2中的位置?我尝试过许多npm方案。但不起作用。如何检查元素是否在视图中?如果元素在视图中,我想在页面上调用API吗?

我有三个标签。我必须检查选项卡是否是活动的,元素是否在视图中。怎么检查?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-05-03 10:12:12

如果元素在viewport上,我使用'jQuery-viewport-checker‘来执行各种任务。对我起作用了。这可能对你有帮助:

如果您没有"jQuery“在您的角2项目中工作:https://stackoverflow.com/a/42295505/7532440,请按此操作

您必须使用'Bower‘安装’checker‘,并将其添加到’ange-cli.json‘文件中的’Script‘中,就像在我提供的链接中安装'jQuery’的方式一样。

cmd:

代码语言:javascript
复制
bower install jQuery-viewport-checker

在“角-cli.json”中:

代码语言:javascript
复制
"scripts": [
      "../bower_components/jquery/dist/jquery.min.js",
      "../bower_components/jQuery-viewport-checker/dist/jquery.viewportchecker.min.js"
  ]

现在您可以使用“jQuery-viewport-checker”。

更多信息:https://github.com/dirkgroenen/jQuery-viewport-checker

您的app.component.ts将如下所示:

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
declare var $:any;

@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css']
})
export class AppComponent {
 title = 'app works!';

 ngOnInit(){

        $(document).ready(function(){
        $("p").click(function(){
        $(this).hide();
    });
    });


        $('.dummy').viewportChecker({
            classToAdd: 'visible', // Class to add to the elements when they are visible,
            classToAddForFullView: 'full-visible', // Class to add when an item is completely visible in the viewport
            classToRemove: 'invisible', // Class to remove before adding 'classToAdd' to the elements
            removeClassAfterAnimation: false, // Remove added classes after animation has finished
            offset: [100], // The offset of the elements (let them appear earlier or later). This can also be percentage based by adding a '%' at the end
            invertBottomOffset: true, // Add the offset as a negative number to the element's bottom
            repeat: false, // Add the possibility to remove the class if the elements are not visible
            callbackFunction: function(elem, action){}, // Callback to do after a class was added to an element. Action will return "add" or "remove", depending if the class was added or removed
            scrollHorizontal: false // Set to true if your website scrolls horizontal instead of vertical.
    });
 }

}

在这里,类‘可见’将添加到".dummy“类中的元素中,一旦它在ViewPort中。您应该根据您的需要对它进行更多的探索(其他参数),因此您可以编写HTML代码。希望能帮上忙。

更新

如果出现错误,请尝试以下操作(因为这些方法适用于问题的作者):

1):尝试将端口ng serve -端口4200更改为4208 (或任何其他端口)

2):在文档中放置viewport检查器代码,如下所示:

代码语言:javascript
复制
jQuery(document).ready(function(){ 
      $('.dummy').viewportChecker({
            classToAdd: 'visible', // Class to add to the elements when they are visible,
            classToAddForFullView: 'full-visible', // Class to add when an item is completely visible in the viewport
            classToRemove: 'invisible', // Class to remove before adding 'classToAdd' to the elements
            removeClassAfterAnimation: false, // Remove added classes after animation has finished
            //offset: [100], // The offset of the elements (let them appear earlier or later). This can also be percentage based by adding a '%' at the end
            invertBottomOffset: true, // Add the offset as a negative number to the element's bottom
            repeat: false, // Add the possibility to remove the class if the elements are not visible
            callbackFunction: function(elem, action){}, // Callback to do after a class was added to an element. Action will return "add" or "remove", depending if the class was added or removed
            scrollHorizontal: false // Set to true if your website scrolls horizontal instead of vertical.
    });
 });

移除偏移量: 100

票数 1
EN

Stack Overflow用户

发布于 2017-05-03 08:44:57

我有一段时间没有使用这个,但可能是您想要的(重构它以满足您的需要):

代码语言:javascript
复制
export function getY(element: HTMLElement) {
  let y = element.offsetTop;

  while (element.offsetParent && element.offsetParent != document.body) {
    element = <HTMLElement>element.offsetParent;
    y += element.offsetTop;
  }
  return y;
}

export function getElementOnScreen(selector: string, delta: number = 0.3): any {
  let windowH = self.innerHeight;
  let windowY = self.pageYOffset;
  let margin = windowH * delta;

  return Array
    .from(document.querySelectorAll(selector))
    .find(el => {
      let elementY = getY(el as HTMLElement);
      let elementH = el.clientHeight;

      let topOnScreen = (elementY - windowY) <= margin;
      let bottomOnScreen = (windowY + margin) <= (elementY + elementH);

      return topOnScreen && bottomOnScreen;
    });
}

export function onScreen$(selector: string): Observable<boolean> {
  return Observable
    .fromEvent(window, 'scroll')
    .throttleTime(100)
    .map(event => getElementOnScreen(selector))

    .do(element => call.api(element))

    .map(Boolean)
    .distinctUntilChanged()
}

示例使用:

代码语言:javascript
复制
<div id="test" [class.i-am-in-viewport]="onScreen$('div#test') |async" />
票数 0
EN

Stack Overflow用户

发布于 2020-05-03 18:03:04

在角度内使用jQuery不是一种很好的方法。

您可以使用交集观察者API来实现这一点。

我用Youtube Iframe创建了一个演示应用程序,它在视频处于视口时播放,当视频不在视口时暂停播放。

工作演示

现场演示:- https://angular-viewport-intersection-observer-youtube.stackblitz.io

直播编辑:- https://stackblitz.com/edit/angular-viewport-intersection-observer-youtube

所用包装

https://github.com/thisissoon/angular-inviewport

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43754551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档