When an element is located beneath the upper div, apply the class "invisible" to hide it

I have a top div on the page with a height of 300px and items generated using ng-repeat. I want to apply the class "no_visible" to each span element if the text "Hello World" is partially or fully hidden under the top div. How can I achieve this?

var app = angular.module('plunker', []);

app
.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.items = [];
  for(var i =0; i<100;i++){
    $scope.items.push(i);
  }
})
.directive('trackVisibility', function(){
  function isVisible(el) {
    var rect = el.getBoundingClientRect();
    var clw = (window.innerWidth || document.documentElement.clientWidth);
    var clh = (window.innerHeight || document.documentElement.clientHeight) ;

    // checks if element is fully visible
    //return (rect.top >= 0 && rect.bottom <= clh) && (rect.left >= 0 && rect.right <= clw);

    // checks if part of element is visible
    return (rect.left <= clw && 0 <= rect.right && rect.top <= clh && 0 <= rect.bottom);

  }
  var reg = [];

  function register(element, fn) {
    reg.push([element, fn]);
  }

  function deregister(element) {
    reg = angular.filter(reg, function (item) {
      return item[0] !== element;
    });
  }

  angular.element(window).on('DOMContentLoaded load resize scroll', function () {
    angular.forEach(reg, function (item) {
        item[1](isVisible(item[0]));
    });
  });

    
  return {
    restrict: 'A',
    link: function (scope, element, attrs, controller) {
      register(element[0], function(isVisible){
        scope.$apply(function(){
          scope.isVisible = isVisible;
        })
      });
      scope.$on('$destroy', function(){
        deregister(element);
      })
    }
  };
});
span {
display: block;
height: 30px;
border: 1px solid;
}
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85e4ebe2f0e9e4f7abeff6c5b4abb7abfd">[email protected]</a>" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js" data-semver="1.2.16"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    
    <div style="height:300px; width:300px; position: fixed; background: #000;">
    </div>
    
    <div style="margin-top:350px; position: absolute;">
    <span style="float:left; clear: both; " ng-repeat="i in items" track-visibility>Hello {{name}} {{isVisible?'yes':'no'}}!</span>
    </div>
    
    
  </body>

</html>

Answer №1

(function(){

angular.module('app')

//Creating a directive named "myElement"
.directive('myElement', function(){

  return {

     restrict : 'E',
     link : function(scope, element, attrs){

      //Performing an action on scroll event

      window.on('scroll', function(){
           var top = $el.getBoundingClientRect().top;  
           if(top < 200){
               //Hide the element
               element.hide();
           }
      }


     }
  };

});
})();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unexpected values being captured by Request.Form from Dynamic Textboxes

I am currently working with dynamic text boxes and retrieving their values in the code behind file. These text boxes are created using Javascript on my ASP.NET page. Here is a snippet of my code: <script type="text/javascript"> $(docume ...

Issues with Ajax POST not functioning correctly in Internet Explorer

A current project of mine involves developing a PHP script that automatically sends birthday emails to individuals. To complement this, I am also working on creating a user-friendly web interface where I can toggle the program on or off, monitor who is cel ...

"Ionic - The $state.go function functions properly during livereload but fails to work on the actual

I've encountered a strange issue with my code. When I use livereload or view the app in a browser, the state.go function functions correctly as expected. However, when I run the app on a device without livereload, the transition to the new state does ...

An uncomplicated broadcasting and receiving method utilizing an event emitter

This code is from chapter 3, example 11 of the book Node.JS in Action, found on page 52. var events = require('events'); var net = require('net'); var channel = new events.EventEmitter(); channel.clients = {}; channel.subscriptions = ...

increase the count by one every second using setInterval

I need my sHand to increase by 6 every second, but currently it only increases once. When I try something like this.sHand++, it works fine and increases by 1 degree per second. However, I want it to increase by 6 instead of 1. Any solutions? data:{ ...

How to access nested array values in React using column references?

I have a specific scenario where I need to display a JavaScript array as a table. Below is the example of the array: var dataArr = [ { Category1: {Input: "5 MB", Output: "6 MB"}, Category2: {Input: "5 MB", Output: "1 MB"}, ...

The TSX file is encountering difficulty rendering an imported React Component

Upon attempting to import the Day component into the Week component (both .tsx files), an error is thrown: Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. ...

The concept of an HTML pop-up message that hovers above the content

While working on my HTML form, I encountered an issue regarding the display of a warning message notifying users about the caps lock being on. Currently, the warning is shown correctly in a div located to the right of the text box. However, this div's ...

Transfer the value of a JavaScript variable to a PHP variable

var javascript_data = $("#ctl00").text(); <?php $php_variable = ?> document.write(javascript_data); <? ; ?> Is there a way to transfer the javascript_data into the php_variable? I'm encountering an issue with this code. Any suggestions? ...

The update operation in MongoDB fails as it requires the _id to be in the form of an ObjectId instead of a

I am currently working on a REST API to facilitate data exchange between a MongoDB database and a web application. The data being exchanged is in JSON format. One issue I am encountering pertains to updating a document: Error: cannot change _id of a docu ...

Having difficulty retrieving controller scope in the view

I am having trouble accessing the scope "$scope.historySelectionFieldValues" in my directive's controller. It does not seem to be populated. Here is my view: View <ji-select-box ng-repeat="{{historySelectionFieldValues}}"></ji-select-box> ...

Using FabricJS ClipTo to Set Image or SVG as Canvas Border

Is there a way to apply the clipTo function to an image or SVG in order to constrain objects within the shape or outline? I'm looking to achieve a similar goal as the user in this post, but the solutions provided were not clear to me. I have success ...

looping through the iteration

Here is a link to my original plunker demonstration: http://plnkr.co/edit/9UBZ9E4uxAo1TXXghm1T?p=preview. In the case of div 4 (ng-if="show==4"), I am looking for a way to hide the particular div when the list is empty. Currently, each div is displayed fo ...

What is the correct way to pass a table record value to a jQuery function?

Within my php code, there is this snippet: echo "<td><button id='pausebut_{$record['idTrack']}' name='StopButton' type='Button' value='{$record['PreviewURL']}'>Stop</button> &l ...

"AngularJS fails to retrieve requested page with HTTP 404 error: Resource not found

After reviewing similar posts on this platform, I am still unable to get my application up and running. To expedite the setup of an end-to-end application, I opted to utilize generator-angular-php. Upon successfully running the application using grunt se ...

A method for altering the values of HTML form checkboxes by toggling between checked and unchecked states, followed by transferring those values to the clipboard

Hello everyone, I'm new to coding and seeking some assistance with a problem I've encountered. My goal is to create a multi-input form with checkboxes, allow the user (me) to fill it out, and then click a "copy" button to transfer the values to t ...

Using V-for in Vue.js to iterate over data sources

I am attempting to display all elements of an array, but currently can only show the first line due to [0]. I want to show all items in the array. <div class="description" v-for="item in sitePartVoice[0].part_attributes"> <small><strong> ...

What is the best way to ensure form submissions in jQuery include non-empty inputs?

Is there a way to only send non-empty inputs (those with values not equal to "") when a user submits a form? I want to achieve this because my website offers forms for filtering data, using the GET method to indicate query strings in the URL. However, som ...

The Vue click event is failing to register when triggered

I am currently facing an issue with a chart nested inside a Vue directive. My goal is to capture the click event, perform some actions, and then pass it through as usual. To achieve this, I have inserted the following code: v-on:click="clickThrough($ ...

Toggle the click event in Javascript to turn it off

I've been busy trying to incorporate multiple expanding divs into a webpage, but I'm struggling to make the divs collapse automatically when another one expands. This is the script I've been experimenting with: $(document).ready(function() ...