What might be causing the in-viewport javascript to not work in my code?

Why is my in-viewport JavaScript code not functioning properly?

Link to JSFiddle code

When the Click to move button is clicked, the cat image will slide correctly.

However, when implementing the following code:

    if($("#testtest").is(":in-viewport"))
    {
        alert("hhh");
    }

To detect when the element with id="testtest" is visible in the viewport and trigger an alert message, nothing seems to happen. How can I fix this issue?

Here is the HTML code snippet:

    <div style="cursor: pointer;" id="move-left" onclick="left_fn(-422)">
        Click to move
    </div>

    <div style=" width: 500px; overflow: hidden; ">
        <ul style=" width: 2000em; transition: transform 200ms ease-in-out; transform: translate3d(0px, 0px, 0px); margin-left: -10px; padding: 0;" id="cat-ul">
            <li style=" display: inline-table; ">
                <img src="http://i.imgur.com/w3abdeV.jpg">
            </li>
            (additional image list items here)
            <li id="testtest" style="width: 1px; display: inline-table;">
            </li>            
        </ul>
    </div>

<script>
function left_fn(value) {
    if($("#testtest").is(":in-viewport"))
    {
        alert("hhh");
    }
    document.getElementById("cat-ul").style.transform = "translate3d("+value+"px, 0px, 0px)";
    var x = (value + 422);
    var y = (value - 422);
    document.getElementById('move-left').setAttribute('onclick','left_fn('+y+')')
}
</script>

Answer №1

In order for this functionality to work, make sure jquery is enabled by visiting this link.

  <script>
function moveLeft(value) {
    if($("#testtest").is(":in-viewport"))
    {
        alert("hhh");
    }
    document.getElementById("cat-ul").style.transform = "translate3d("+value+"px, 0px, 0px)";
    var x = (value + 422);
    var y = (value - 422);
    document.getElementById('move-left').setAttribute('onclick','left_fn('+y+')')
}
</script>

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

The JSON data lacks compatibility with the usage of track by functionality

I am working on a project that involves three connected select menus. The first one displays series names, the second one displays chapter numbers belonging to the series selected in the first menu, and the third one displays page numbers belonging to t ...

Discover ways to circumvent using multiple conditions in a switch statement for a solitary object

Receiving an object from the client side in this format: var condition={"bedrooms":"1,2,3,4","Inhibition":"1,6","possession":"3","id":"8",toilets:"1,2",...,} The object must have the same keys and only be a single object, but its length can vary (1/2 ...

Having trouble accessing a parsed JSON value in Node.js, encountering 1 error

Currently, I am working on creating a simple news web application as a way to practice utilizing APIs. The particular API that I have chosen for this project is . If you take a look at this JSON object that I am attempting to console.log, you will see the ...

execute the NPM script in the current directory

Within my package.json file for a node project, I have the following test script that utilizes the ts-node package: "scripts": { "build": "tsc", "test": "ts-node" } Executing this script from the root ...

Encountered an xHTML error during a jQuery AJAX call that resulted in a "mismatched tag, expected: </br>" message

My issue seems simple on the surface, but I need someone with a deep understanding to help. Picture this straightforward task: loading and parsing external HTML data into an element within the current HTML file. Let's consider the content of the exte ...

Troubleshooting: Images not displaying properly in React due to Webpack configuration bottleneck,

I am facing an issue with loading images in my React application. Some images are set up in the CSS file like this: background-image: url('/../img/logo.png'); On the other hand, I would like to use images inside React components using the img ...

Vue.JS - Dynamically Displaying Property Values Based on Other Property and Concatenating with

I have a reusable component in Vue.js called DonutChart. <donut-chart :chartName="graphPrefix + 'PerformanceDay'" /> The value of the property graphPrefix is currently set to site1. It is used as part of the identifier for the div id ...

Ways to invoke a JavaScript function within a child window that is already open

I am working on a project where I have a main html file. In this file, I want the user to click on something that will trigger a new window or tab to open. This new window/tab should display the dynamically generated content of a hidden div in the main htm ...

I have difficulty generating appropriate pseudonyms

Struggling to create aliases in my react project (CRA 3.1.1) has been a challenge for me. Despite attempting various methods, I have not been successful in achieving it. The only success I've had so far is aliasing the simple "src" folder based on som ...

Learning to control the JavaScript countdown clock pause and play functionality

How can I control the countdown timer to play and pause, allowing me to resume at the exact time it was paused? At the start, the timer is set to play. Please keep in mind that the button appears empty because the font-awesome package was not imported, b ...

Creating a universal header for all webpages in Angular JS by dynamically adding elements using JavaScript DOM manipulation techniques

I have successfully created a json File containing an array of "learnobjects", each including an id, name, and link. Check out my plnkr example var myMessage = { "learnobjects": [{ "id": "1", "name": "Animation-Basics", "link": "animation_bas ...

The React Native File generator

Currently, we are utilizing redux actions in our web project. In an effort to share logic between web and native applications, we have integrated these actions into our react native project with the intention of only having to modify the components. One o ...

Saving a session variable to the database using ajax technology

If you're like me and struggling to figure out how to add a session variable (username) to a database using ajax, I've been there. My limited knowledge of jQuery and JavaScript made it quite the challenge. The dynamic table I'm working with ...

Does the "too much recursion error" result from the if clause?

Before I send my form data to a PHP script, I am performing a jQuery check: $(document).ready(function(){ $("#submit").submit(function(e){ var vari1 = $("#inputhidden1").val(); var vari2 = $("#inputhidden2").val(); if(vari1 == 1) { ...

What is the best way to use CSS to center two blocks with space in between them?

My goal is to achieve a specific design: I want two text blocks with some space in between them aligned around the midline of the page (refer to the image). I have experimented with the float property, as well as used margin and padding to create the gap ...

Having trouble getting the Random Function to function correctly in Discord.js

I'm working on a piece of code that generates a random value to select two different values from two separate arrays. Every time I run it, however, the result seems to be the same and not truly random. I'm not sure where I went wrong with this. I ...

What is the best way to restrict datalist options while preserving the "value" functionality?

After finding a creative solution by @Olli on Limit total entries displayed by datalist, I successfully managed to restrict the number of suggestions presented by a datalist. The issue arises from the fact that this solution only covers searching through ...

What is the best way to eliminate the space between columns?

I have a simple structure on BootStrap. <div class="container"> <div class="row"> <div class="col-md-4 blue-section" ><p>Some content</p></div> <div class="col-md-4 red-section"><p>Some content&l ...

How can you effectively implement multiple ajax requests with jQuery?

i am facing a situation where I have different forms in various PHP files: <form id="s"> <input id="q"/> <input type="submit" href="Search!"/> </form> <div id="r"></div> Within foo.php, there is another form: <form ...

Scrolling vertically without the appearance of a scrollbar

I've searched all over the internet for a solution to my problem, but nothing seems to work for me. I want to keep vertical scrolling ability while hiding the scrollbar from view in the y direction all the time. The challenge is to make my #content-m ...