What is the method to convert Javascript values from pixels to percentages?

Is it possible to change the scrolltop value dynamically based on a percentage of the user's screen size? I've been trying to achieve this using JS but haven't had any luck. Here is a link to a codepen that showcases the issue:

[link] (http://codepen.io/ericshio/pen/zBRbAY)

Here is the HTML:

<div class="filler"></div>
<a href="#introjump"><img class="down-arrow" src="http://www.themainconcept.com/wp-content/uploads/2015/11/down-arrow-wht.png" alt="down arrow wht"/></a>

This is the CSS used:

.down-arrow {
    position: fixed;
    bottom: 1%;
    left: 50%;
    max-width: 3.5%;
    min-width: 3.5%;
    width: 3.5%;
    box-shadow: none;
    opacity: 0.6;
}

.down-arrow:hover {
    opacity: 1;

}


.filler {
    height: 10000px;
}

And here is the JavaScript being applied:

$(window).scroll(function() {
  $(".down-arrow").css("opacity", 1 -
    $(window).scrollTop() / 50);
});

Answer №1

Give this a shot.

When you scroll, the code below will adjust the opacity of the ".down-arrow".
$(window).scroll(function() {
  $(".down-arrow").css("opacity", 1 -
    $(window).scrollTop() / $(document).height()*0.5);
});

Answer №2

give this a shot

.up-arrow {
    position: fixed;
    top: 10%;
    right: 50%;
    max-width: 5%;
    min-width: 5%;
    width: 5%;
    box-shadow: none;
    opacity: 0.8;
}

.up-arrow:hover {
    opacity: 1;
}


.filler-div {
    height: 70%;
}

Answer №3

It appears that the intention is to gradually fade out the arrow as the user scrolls down

$(window).scroll(function() {
  $(".down-arrow").css("opacity", 1 - ($(window).scrollTop() / document.body.scrollHeight));
});
.down-arrow {
  position: fixed;
  bottom: 1%;
  left: 50%;
  max-width: 3.5%;
  min-width: 3.5%;
  width: 3.5%;
  box-shadow: none;
  opacity: 0.6;
}
.down-arrow:hover {
  opacity: 1;
}
.filler {
  height: 10000px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="filler"></div>
<a href="#introjump">
  <img class="down-arrow" src="http://www.themainconcept.com/wp-content/uploads/2015/11/down-arrow-wht.png" alt="down arrow wht" />
</a>

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

What is the best way to send selected checkboxes from a form to a PHP page in the form of an array using a jQuery submit feature?

Currently, I am working on a project that involves a list of checkboxes for user selection. The checkboxes are dynamically generated from a mySQL query, allowing users to select multiple options. Here is the code snippet for generating the checkboxes: ech ...

In what scenarios would the outcomes differ between the following two approaches to binding a click event to a paragraph?

When might the following two approaches to binding a click event on a paragraph exhibit different behaviors: $("p").on("click", function() { alert("p clicked") }) $("p").click(function(){ alert("p clicked") }); ...

What is the best way to extract all the data from a JSON using a shared key?

I'm trying to extract an array containing all the name values from a JSON structure. This is how my JSON data is structured: { "profile": { "G5j7": { "name": "siddharth", "age": &quo ...

Issues persist with $.getJSON

I apologize if this question has been addressed previously, but I couldn't find the answer I was seeking. My issue involves using $.getJSON to send variables to a PHP file. Although the file returns success as true, it always triggers the .fail functi ...

Generating examples of two models that are interdependent

In my Javascript form, I have implemented an AJAX POST request that successfully creates a new instance of a model called Component. Now, my goal is to allow users to input keywords for the Component model through the same form. To achieve this, I have al ...

Utilize AJAX in JavaScript file

I am encountering an issue with the following code: function inicioConsultar(){ $(function(){ $('#serviciosU').change(function(){ if ($('#serviciosU').val()!= "-1") { $.ajax({ url: "@Url. ...

Tips for displaying an element in a fresh container using jQuery

I am looking to add draggable functionality to the objects in columns on my page. Below is the script I have implemented: <script> $(document).ready(function () { $('.application').draggable({ helper: 'clone' }); ...

Executing functions in real-time using React Native

I'm fairly new to Object-Oriented Programming (OOP) and my understanding of promises, asynchronous/synchronous function execution is quite basic. Any guidance or help from your end would be greatly appreciated! Let's take a look at an example fr ...

Retrieve the Object from the array if the input value is found within a nested Array of objects

Below is the nested array of objects I am currently working with: let arrayOfElements = [ { "username": "a", "attributes": { roles:["Tenant-Hyd"], groups:["InspectorIP", "InspectorFT"] } }, { ...

Understanding how to access and process data submitted to classic ASP through jQuery AJAX

Below is the Javascript code that I have written: function sendCcbRequest(text) { var jsonToSend = "\"text\": \"" + escape(text) + "\""; $.ajax({ type: "POST", url: 'x.asp', data: jsonToSend, ...

Learn how to set up webpack or Next.js to generate a CSS file from custom Material styles and automatically inject it into the header

One of the things I've done is define some custom material styles, like this: import {createStyles, makeStyles, Theme} from '@material-ui/core/styles'; export const useStyles = makeStyles((theme: Theme) => createStyles({ fab ...

Retrieve JSON data using AngularJS

Can someone guide me on how to make a GET request to my API endpoint and handle the JSON response in my code? Sample Controller.js Code: oknok.controller('listagemController', function ($scope, $http) { $scope.init = function () { ...

"An error occurred due to the index 'custom_css'

Currently utilizing WordPress version 3.8.1 During my backup process, I have set the delivery method to email. However, despite no changes being made to the site, the last successful backup was on 3/6/14. I do not receive the email and encounter the error ...

Tips for hiding a div only on mobile devices while always displaying it on large screens using AngularJS or CSS

How can I use Angularjs or css to hide a div on mobile devices only, ensuring that it is always displayed on large screens? ...

What is the best way to initialize React-map-gl with the user's current location as the default latitude and longitude?

Is there a way to render the map with default values of viewport set to user's location without needing to click a locate me button? <ReactMapGL mapboxApiAccessToken={mapboxApiKey} mapStyle="mapbox://styles/mapbox/streets-v11" ...

Resize the tab header in primefaces to fit your needs

Is there a way to change the height of the tabView's header specifically in primefaces? Take for instance the tabs with headers like "God Father I", "God Father II" on this link, I only want to adjust the height of the header and not the entire tab. ...

Observing Gulp's behavior is quite peculiar - it seems to be running

I ran into a rather peculiar issue with gulp watch. In my system, I have two disk drives: D and E. Disk D is a section of my computer's hard disk drive, while disk E is a flash drive. Both disks contain a file named test.txt: D:\test\test ...

What is the best way to enhance a React Native component's properties with Flow?

I'm currently working with Flow version 0.54.1 in my React Native project, specifically using version 0.48.3. I have developed a component named PrimaryButton that acts as a wrapper for the native TouchableOpacity component. My goal is to define custo ...

The function of jQuery .prepend allows for content to

Currently, I am using PHP to create dynamic HTML elements. <?php $news = retrieveNewsData(); foreach($news as $single_news) { echo $single_news[0] . " - " . $single_news[2] . "\n"; } ?> My goal is to place the generated content into a spe ...

There appears to be an issue with reading the property 'toLowerCase' of an undefined element, and I'm having trouble identifying the error

The variables I have initialized (globally): var audio; var LANGUAGE; var audioArray; var MEDIAARRAY; var WORDS; var SOUNDARRAY; This specific line is causing the error: var audioId = MEDIAARRAY.audio.lowercase.indexOf(exObject['exerciseGetWordInpu ...