Files
What_Are_You_Lifting_App/js/Weight Calculation Script-Edited-Jquery-V4.js
Steven Haskell 40f0c47972 Attempted restore scroll script added favicons
Tried to add the return scroll for  when the focus left on mobile. Still need to figure it out.

Added favicons.
2018-10-18 00:52:55 -04:00

155 lines
5.1 KiB
JavaScript

// jQuery( document ).ready(function() {
// calculation function starts here
function startcalc() {
jQuery(".input-weightsneeded").val(null);
//calls inputs to start calculation
var weighttolift = jQuery(".input-weight").val();
var percentage = jQuery(".input-percentage").val();
var bar = jQuery(".input-bar").val();
// calculates and logs final values
var final = Math.floor(weighttolift * percentage);
// calculates weight of the plates needed
var finalPlateWeights = final - bar;
//This creates an arrary for the weights available
var plateWeights = [];
$( ".input-weightsavailable" ).each( function(){
plateWeights.push( $( this ).data( "weight" ));
});
//This creates an arrary for the number of plates available
var platesAvailable = [];
$( ".input-weightsavailable" ).each( function(){
platesAvailable.push( $( this ).val());
});
//calls the plate calculator function
platecalculator(plateWeights, platesAvailable, finalPlateWeights, final);
}
// var plateloader = jQuery(".b-platevisual__plates").html('<img src="images/45lbs-Plate.svg" class="b-platevisual__plate b-platevisual__plate--45">');
// console.log(plateloader);
//plate calulation function
function platecalculator ( plateWeights, platesAvailable, target, finalWeight) {
var weighthtml = "";
//iterates though the plates availble.
for (var i = 0; i < platesAvailable.length; i++) {
//checks if the plate is less than or equal to the weight of plates needed
if (plateWeights[i]*2 <= target) {
var weightToAdd = Math.floor(target / (plateWeights[i]*2));
//check if there are enough weights
var plateCheck = Math.floor(platesAvailable[i]/2);
if (weightToAdd > plateCheck) {
weightToAdd = plateCheck;
}
//subtracts added weights
target = target - weightToAdd*(plateWeights[i]*2);
//Adds plates required
if (weightToAdd > 0) {
jQuery(".input-weightsneeded").eq(i).val(weightToAdd);
//itterates through plates being added and saves html to string
for(var x = 0; x < weightToAdd; x++){
//instead of putting in a string and updating all at once. append the weights each time. Make function and append and then animate. Replace this block.
var platename = plateWeights[i];
if (platename == 2.5) {
platename = '2half';
}
weighthtml += '<img src="images/'+ platename +'lbs-Plate.svg" class="b-platevisual__plate b-platevisual__plate--' + platename + '">';
}
}
//sets the offset
jQuery(".input-difference").text(target);
//sets the final weight
jQuery(".input-final").text(finalWeight - target);
}
}
//replaces html for plates required to bar.
jQuery(".b-platevisual__plates").html(weighthtml);
}
//Smooth Scrolling
$(document).ready(function(){
// Add smooth scrolling to all links
$("a").on('click', function(event) {
// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
});
//Saves scroll position on focus and restores
var savedScrollTop
function saveScroll() {
var savedScrollTop = jQuery(document).scrollTop(); // save scroll position
console.log(savedScrollTop);
}
function restoreScroll(savedScrollTop){
jQuery("html, body").animate({
scrollTop: savedScrollTop
}, 800).offset().top;
}
//Failed scroll restore functions
// jQuery("#Weight").blur(function(event) {
// $("html, body").animate({
// scrollTop: savedScrollTop
// }, 800).offset().top;
// });
/*function restoreScroll() {
$("a").on('click', function(event) {
jQuery(window).scrollTop(savedScrollTop);*/
/* window.scrollTo({
top: "savedScrollTop",
behavior: "smooth"
}); // restore it*/
/*var savedScrollTop = $(document).scrollTop(); // save scroll position
<code that changes focus goes here>
$(document).scrollTop(savedScrollTop ); // restore it*/
// });