Why is scroll-behavior not working on my phone? - html

I added the scroll-behavior property in my CSS but it only works on the google chrome tool that allows me to see my website like if it was on a phone. When I load my website on my actual phone, there is no sliding effect. Any idea?
Here is what the code looks like :
function scrollToSection(event) {
if (supportsSmoothScrolling()) {
return;
}
event.preventDefault();
const scrollToElem = document.getElementById("section");
SmoothVerticalScrolling(scrollToElem, 300, "top");
}
function supportsSmoothScrolling() {
const body = document.body;
const scrollSave = body.style.scrollBehavior;
body.style.scrollBehavior = 'smooth';
const hasSmooth = getComputedStyle(body).scrollBehavior === 'smooth';
body.style.scrollBehavior = scrollSave;
return hasSmooth;
};
function SmoothVerticalScrolling(element, time, position) {
var eTop = element.getBoundingClientRect().top;
var eAmt = eTop / 100;
var curTime = 0;
while (curTime <= time) {
window.setTimeout(SVS_B, curTime, eAmt, position);
curTime += time / 100;
}
}
function SVS_B(eAmt, position) {
if (position == "center" || position == "")
window.scrollBy(0, eAmt / 2);
if (position == "top")
window.scrollBy(0, eAmt);
}
html, body {
scroll-behavior: smooth;
overflow-y: scroll;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="container-fluid h-100 nopadding">
<div class="row h-100 nopadding">
<div class="col-lg-8 black nopadding text-center d-flex flex-column justify-content-center">
<a onclick="scrollToSection(event)" href="#more" class="more">Here is the button</a>
</div>
<div id="more"
class="col-lg-4 white nopadding text-center d-flex flex-column align-self-center justify-content-center">
Here is the section
</div>
</div>
</div>
I didn't add all the page elements but the arrow represent the anchor tag in the code :

As you have noticed, Safari does not support the scroll-behavior CSS property (see this). You'd need some JavaScript to achieve the same effect across different browsers. You may find an example here.
Update:
This is a working code snippet of this answer (which combines the answers of George Daniel and terrymorse).
function scrollToSection(event) {
if (supportsSmoothScrolling()) {
return;
}
event.preventDefault();
const scrollToElem = document.getElementById("section");
SmoothVerticalScrolling(scrollToElem, 300, "top");
}
function supportsSmoothScrolling() {
const body = document.body;
const scrollSave = body.style.scrollBehavior;
body.style.scrollBehavior = 'smooth';
const hasSmooth = getComputedStyle(body).scrollBehavior === 'smooth';
body.style.scrollBehavior = scrollSave;
return hasSmooth;
};
function SmoothVerticalScrolling(element, time, position) {
var eTop = element.getBoundingClientRect().top;
var eAmt = eTop / 100;
var curTime = 0;
while (curTime <= time) {
window.setTimeout(SVS_B, curTime, eAmt, position);
curTime += time / 100;
}
}
function SVS_B(eAmt, position) {
if (position == "center" || position == "")
window.scrollBy(0, eAmt / 2);
if (position == "top")
window.scrollBy(0, eAmt);
}
body {
scroll-behavior: smooth;
height: 1000px;
}
section {
margin-top: 500px;
}
<a onclick="scrollToSection(event)" href="#section">
Redirect On section
</a>
<section id="section">
Section Content
</section>

Related

Couldn't change the text between a Html element using JQuery

I have the JQuery code below. I changed the text between a button element from + to x in second if statement. Now, when the button is displaying x, I wanted to change the text to + in first if statement using $(this).text("+");, yet the button is displaying x even after the change. I have checked if the text was changed to + using console.log() and it says it has changed to +. But, why isn't the result showing its effect on the button?
let players = [];
let remainingPlayers = 11;
$(".card > button").click(function(e) {
let removePlayer = "";
let index = -1;
if ($(this).text() !== "+") {
removePlayer = $(this).attr("value");
index = players.indexOf(removePlayer);
if (index != -1) {
$(this).text("+");
players.splice(index, 1);
console.log(players);
console.log($(this).text());
}
}
if (remainingPlayers != 0) {
remainingPlayers -= 1;
players.push($(this).attr("value"));
$(this).text("x");
$(this).addClass("btn btn-danger button-select-players");
$("#pl-remain").text(remainingPlayers);
} else {
alert("You can't select more players");
console.log(players);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-lg-6 col-md-12 col-sm-12">
<div class="card" style="width: 15rem;">
<img src="..." class="card-img-top" alt="...">
<button class="btn btn-success button-select-players" type="button" name="button" value="xyz">+</button>
<ul class="list-group list-group-flush">
<li class="first-li list-group-item">Name</li>
<li class="list-group-item">0</li>
</ul>
</div>
</div>

How to write jquery function for previous button?

I have created a step form with HTML & jquery which has next and previous buttons for navigating to different steps. I have written jquery function for next buttons, but could'nt do one for previous buttons. Please help me with code.
code to trigger next button
var allNextBtn = $('.nextBtn');
allNextBtn.click(function () {
var curStep = $(this).closest(".setup-content"),
curStepBtn = curStep.attr("id"),
nextStepWizard = $('div.setup-panel div a[href="#' + curStepBtn + '"]').parent().next().children("a"),
curInputs = curStep.find("input[type='text'],input[type='url']"),
isValid = true;
$(".form-group").removeClass("has-error");
for (var i = 0; i < curInputs.length; i++) {
if (!curInputs[i].validity.valid) {
isValid = false;
$(curInputs[i]).closest(".form-group").addClass("has-error");
}
}
if (isValid) nextStepWizard.removeAttr('disabled').trigger('click');
});
I would suggest another approach to your problem:
You want to create a stepper
In the stepper, you would like to show pages of a form
The stepper needs to have controls: prev, next (, first, last?)
The controls should take form validation into account (on the current page)
You want to do DOM manipulation with jQuery
Consider the following snippet:
var currentPage = 1
jQuery(document).ready(function($) {
// init first page
updatePage(0)
jQuery(".btn.next").on('click', function(e) {
e.preventDefault()
// checking for last item & if page input fields are valid
const direction = ($('.page').length > currentPage && validatePage()) ? 1 : 0
updatePage(direction)
})
jQuery(".btn.prev").on('click', function(e) {
e.preventDefault()
// checking for fist item
const direction = 1 < currentPage ? -1 : 0
updatePage(direction)
})
})
// simple validation function
function validatePage() {
const inputValue = jQuery(`[data-page-order="${ currentPage }"]`).find('input').val()
return inputValue !== ''
}
// simple page visibility update function
function updatePage(direction) {
if (direction === 1 || direction === -1) {
currentPage += direction
$(".page.visible").fadeOut().removeClass('visible')
$(`[data-page-order="${ currentPage }"]`).fadeIn().addClass('visible')
} else {
$(".page.visible").removeClass('visible')
$(`[data-page-order="${ currentPage }"]`).addClass('visible')
}
}
form {
width: 50%;
}
.pages {
position: relative;
height: 50px;
background-color: rgba(0, 0, 0, 0.2);
display: flex;
justify-content: center;
align-items: center;
}
.pages>.page {
display: block;
position: absolute;
display: none;
}
.pages>.page.visible {
display: block;
}
.controls {
display: flex;
justify-content: space-between;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<form>
<div class="pages">
<div class="page" data-page-order="1">
<label for="input1">Input 1:
<input id="input1" type="text" />
</label>
</div>
<div class="page" data-page-order="2">
<label for="input2">Input 2:
<input id="input2" type="text" />
</label>
</div>
<div class="page" data-page-order="3">
<label for="input3">Input 3:
<input id="input3" type="text" />
</label>
</div>
</div>
<div class="controls">
<button class="btn prev">PREV</button>
<button class="btn next">NEXT</button>
</div>
</form>
</div>
You can see that with the manipulation of a single variable (currentPage) prev & next steps can be done - with validation rules, form pages appear/disappear with a fadeIn/fadeOutand all.
I do hope that this snippet gives you the idea about how to solve a problem like this - break it down to requirements that are simple and then don't overcomplicate :) Just add one feature at a time (like prev/next, validation, etc.).

How to style an element only if it contains a specific element with a specific class [duplicate]

This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 4 years ago.
I have the following element:
<div class="parent">
<div class="col">MyColumn</div>
</div>
I want to style the parent class only if it contains a class with class col
Threw together a fairly legacy compatible JavaScript version:
var parents = document.querySelectorAll('.parent')
for (var i = 0; i < parents.length; i++) {
for (var j = 0; j < parents[i].children.length; j++) {
if (hasClass(parents[i].children[j], 'col')) {
updateCss(parents[i])
}
}
}
function hasClass(element, className) {
return (' ' + element.className + ' ').indexOf(' ' + className+ ' ') > -1;
}
function updateCss (parent) {
// whatever you want to add here...
parent.style.color = 'red';
}
.parent {
margin: 15px;
outline: 1px dotted black;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="index.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="parent">
<div class="col">MyColumn</div>
</div>
<div class="parent">
<div class="something-else">not column</div>
<div class="col">MyColumn</div>
</div>
<div class="parent">
<div class="something-else">not column</div>
</div>
<script src="index.js"></script>
</body>
</html>
In JQuery you can use the :has selector, like this:
$('.parent:has(.col)').css(/* your css */);
Below are two options to do it using jQuery:
Option 1
$('.parent').has('.col').addClass('style');
.style {
background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
<div class="col">MyColumn</div>
</div>
Option 2
if ($(".col").parents(".parent").length == 1) {
// add style here
$(".parent").css({'background': "red"});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
<div class="col">MyColumn</div>
</div>
this is what I've come up with so far. what I'm trying to do is to spread the div's with class COL in the remaining space after the size specific col's.
it's still not perfect and needs a little tweaking, but a step in the right direction.
var mediaBase = parseFloat(8.33333);
$.each($("div.row"), function (index) {
var gens = 0;
var total = 0;
$.each($(this).find("div[class^='col']"), function (ii) {
if (total > 100) {
$(this).hide();
}
else {
if ($(this).attr("class").indexOf("-") > 0) {
let tmp = $(this).attr("class");
tmp = parseInt(tmp.slice(tmp.lastIndexOf("-") + 1));
total += parseFloat(mediaBase * tmp);
}
else {
gens++;
}
}
});
if (gens > 0 && total < 100) {
let val = (100 - total) / gens;
if (total === 0) {
val = 100 / gens;
}
$(this).find("div.col").css("width", val.toString() + "%");
}
});

PhotoSwipe with Polymer misalignment

I'm trying to make a small web-app using Polymer, based on the Nav + List + Detail-layout example they have provided. For part of my web-app I want to make a photo gallery. Luckily, there's an element for that. Unfortunately, there's no element for that. So I set myself upon the task of making my own custom-element that provides this functionality, based on PhotoSwipe.
So I decided to start simple by just implementing the code they provided as an example (see this CodePen). I simply copied this code into a custom element to see how it works, but unfortunately it doesn't work 100%. Here is the CodePen of the custom element.
For small displays the photo's display correctly, but when the screen is tall the 3rd and 4th photo don't align to the center anymore, but slide to the bottom right corner. The easiest way to see this is by pressing the full screen button when viewing an image. Below is an image where you can clearly see the offset to the bottom-right.
I rechecked and this isn't an issue in their own CodePen, but I can't seem to find if there are overlapping styles or something else is breaking it up. Any ides?
<dom-module id="photo-album">
<link rel="import" type="css" href="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.0/photoswipe.css"> <!-- photoswipe/photoswipe.css -->
<link rel="import" type="css" href="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.0/default-skin/default-skin.css"> <!-- photoswipe/default-skin/default-skin.css -->
<template>
<style>
:host {
display: block;
}
.my-gallery {
width: 100%;
float: left;
}
.my-gallery img {
width: 100%;
height: auto;
}
.my-gallery figure {
display: block;
float: left;
margin: 0 5px 5px 0;
width: 150px;
}
.my-gallery figcaption {
display: none;
}
</style>
<iron-ajax url="" params="" handle-as="json" last-response="{{photos}}"></iron-ajax>
<!--<template is="dom-repeat" items="{{photos}}">
<iron-image style="width:400px; height:400px; background-color: lightgray;" sizing="cover" preload fade src="{{item}}"></iron-image>
</template>-->
<div class="my-gallery" id="gallery" itemscope itemtype="http://schema.org/ImageGallery">
<figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
<a href="https://farm3.staticflickr.com/2567/5697107145_a4c2eaa0cd_o.jpg" itemprop="contentUrl" data-size="1024x1024">
<img src="https://farm3.staticflickr.com/2567/5697107145_3c27ff3cd1_m.jpg" itemprop="thumbnail" alt="Image description" />
</a>
</figure>
<figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
<a href="https://farm2.staticflickr.com/1043/5186867718_06b2e9e551_b.jpg" itemprop="contentUrl" data-size="964x1024">
<img src="https://farm2.staticflickr.com/1043/5186867718_06b2e9e551_m.jpg" itemprop="thumbnail" alt="Image description" />
</a>
</figure>
<figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
<a href="https://farm7.staticflickr.com/6175/6176698785_7dee72237e_b.jpg" itemprop="contentUrl" data-size="1024x683">
<img src="https://farm7.staticflickr.com/6175/6176698785_7dee72237e_m.jpg" itemprop="thumbnail" alt="Image description" />
</a>
</figure>
<figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
<a href="https://farm6.staticflickr.com/5023/5578283926_822e5e5791_b.jpg" itemprop="contentUrl" data-size="1024x768">
<img src="https://farm6.staticflickr.com/5023/5578283926_822e5e5791_m.jpg" itemprop="thumbnail" alt="Image description" />
</a>
</figure>
</div>
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
<div class="pswp__bg"></div>
<div class="pswp__scroll-wrap">
<div class="pswp__container">
<div class="pswp__item"></div>
<div class="pswp__item"></div>
<div class="pswp__item"></div>
</div>
<div class="pswp__ui pswp__ui--hidden">
<div class="pswp__top-bar">
<div class="pswp__counter"></div>
<button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
<div class="pswp__preloader">
<div class="pswp__preloader__icn">
<div class="pswp__preloader__cut">
<div class="pswp__preloader__donut"></div>
</div>
</div>
</div>
</div>
<div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
<div class="pswp__share-tooltip"></div>
</div>
<button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)"></button>
<button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)"></button>
<div class="pswp__caption">
<div class="pswp__caption__center"></div>
</div>
</div>
</div>
</div>
</template>
<script src="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.0/photoswipe.min.js"></script> <!-- photoswipe/photoswipe.min.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.0/photoswipe-ui-default.min.js"></script> <!-- photoswipe/photoswipe-ui-default.min.js -->
<script>
Polymer({
is: 'photo-album',
properties: {
items: {
type: Array,
notify: true,
}
},
ready: function() {
this.initPhotoSwipeFromDOM();
},
initPhotoSwipeFromDOM: function(){
var gallerySelector = this.$.gallery;
var parseThumbnailElements = function(el) {
var thumbElements = el.childNodes,
numNodes = thumbElements.length,
items = [],
figureEl,
linkEl,
size,
item;
for(var i = 0; i < numNodes; i++) {
figureEl = thumbElements[i]; // <figure> element
// include only element nodes
if(figureEl.nodeType !== 1) {
continue;
}
linkEl = figureEl.children[0]; // <a> element
size = linkEl.getAttribute('data-size').split('x');
// create slide object
item = {
src: linkEl.getAttribute('href'),
w: parseInt(size[0], 10),
h: parseInt(size[1], 10)
};
if(figureEl.children.length > 1) {
// <figcaption> content
item.title = figureEl.children[1].innerHTML;
}
if(linkEl.children.length > 0) {
// <img> thumbnail element, retrieving thumbnail url
item.msrc = linkEl.children[0].getAttribute('src');
}
item.el = figureEl; // save link to element for getThumbBoundsFn
items.push(item);
}
return items;
};
// find nearest parent element
var closest = function closest(el, fn) {
return el && ( fn(el) ? el : closest(el.parentNode, fn) );
};
// triggers when user clicks on thumbnail
var onThumbnailsClick = function(e) {
e = e || window.event;
e.preventDefault ? e.preventDefault() : e.returnValue = false;
var eTarget = e.target || e.srcElement;
// find root element of slide
var clickedListItem = closest(eTarget, function(el) {
return (el.tagName && el.tagName.toUpperCase() === 'FIGURE');
});
if(!clickedListItem) {
return;
}
// find index of clicked item by looping through all child nodes
// alternatively, you may define index via data- attribute
var clickedGallery = clickedListItem.parentNode,
childNodes = clickedListItem.parentNode.childNodes,
numChildNodes = childNodes.length,
nodeIndex = 0,
index;
for (var i = 0; i < numChildNodes; i++) {
if(childNodes[i].nodeType !== 1) {
continue;
}
if(childNodes[i] === clickedListItem) {
index = nodeIndex;
break;
}
nodeIndex++;
}
if(index >= 0) {
// open PhotoSwipe if valid index found
openPhotoSwipe( index, clickedGallery );
}
return false;
};
// parse picture index and gallery index from URL (#&pid=1&gid=2)
var photoswipeParseHash = function() {
var hash = window.location.hash.substring(1),
params = {};
if(hash.length < 5) {
return params;
}
var vars = hash.split('&');
for (var i = 0; i < vars.length; i++) {
if(!vars[i]) {
continue;
}
var pair = vars[i].split('=');
if(pair.length < 2) {
continue;
}
params[pair[0]] = pair[1];
}
if(params.gid) {
params.gid = parseInt(params.gid, 10);
}
return params;
};
var openPhotoSwipe = function(index, galleryElement, disableAnimation, fromURL) {
var pswpElement = document.querySelectorAll('.pswp')[0],
gallery,
options,
items;
items = parseThumbnailElements(galleryElement);
// define options (if needed)
options = {
// define gallery index (for URL)
galleryUID: galleryElement.getAttribute('data-pswp-uid'),
getThumbBoundsFn: function(index) {
// See Options -> getThumbBoundsFn section of documentation for more info
var thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnail
pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
rect = thumbnail.getBoundingClientRect();
return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
}
};
// PhotoSwipe opened from URL
if(fromURL) {
if(options.galleryPIDs) {
// parse real index when custom PIDs are used
// http://photoswipe.com/documentation/faq.html#custom-pid-in-url
for(var j = 0; j < items.length; j++) {
if(items[j].pid == index) {
options.index = j;
break;
}
}
} else {
// in URL indexes start from 1
options.index = parseInt(index, 10) - 1;
}
} else {
options.index = parseInt(index, 10);
}
// exit if index not found
if( isNaN(options.index) ) {
return;
}
options.showAnimationDuration = 0;
options.hideAnimationDuration = 0;
// Pass data to PhotoSwipe and initialize it
gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();
};
// loop through all gallery elements and bind events
var galleryElements = this.$.gallery;
galleryElements.setAttribute('data-pswp-uid', 1);
galleryElements.onclick = onThumbnailsClick;
// Parse URL and open gallery if it contains #&pid=3&gid=1
var hashData = photoswipeParseHash();
if(hashData.pid && hashData.gid) {
openPhotoSwipe( hashData.pid , galleryElements[ hashData.gid - 1 ], true, true );
}
}
});
</script>
</dom-module>
It seems you're missing styles from photoswipe.css.

Boostrap responsive grid issue with different images size

this is my Boostrap grid:
http://i59.tinypic.com/nozn12.png
Each image has different sizes, unfortunately the heightest one wraps the row leaving an empty space.
I should give the same size in each image, but I can't because this isn't a fixed layout.
Is there any fix / solution to this issue? Here's the code
<div class="row">
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<img src="temp/1.jpg" class="img-responsive">
<h4>4280 / 5PL</h4>
</div>
</div>
<div class="col-sm-6 col-md-3">
ecc.
</div>
<div class="col-sm-6 col-md-3">
ecc.
</div>
</div>
use equal height for <div class="col-sm-6 col-md-3">
$.fn.eqHeights = function(options) {
var defaults = {
child: false
};
var options = $.extend(defaults, options);
var el = $(this);
if (el.length > 0 && !el.data('eqHeights')) {
$(window).bind('resize.eqHeights', function() {
el.eqHeights();
});
el.data('eqHeights', true);
}
if( options.child && options.child.length > 0 ){
var elmtns = $(options.child, this);
} else {
var elmtns = $(this).children();
}
var prevTop = 0;
var max_height = 0;
var elements = [];
elmtns.height('auto').each(function() {
var thisTop = this.offsetTop;
if (prevTop > 0 && prevTop != thisTop) {
$(elements).height(max_height);
max_height = $(this).height();
elements = [];
}
max_height = Math.max(max_height, $(this).height());
prevTop = this.offsetTop;
elements.push(this);
});
$(elements).height(max_height);
};
// run on load so it gets the size:
// can't have the same pattern for some reason or it scans the page and makes all the same height. Each row should be separate but it doesn't work that way.
$(window).load(function() {
//$('[class*="eq-"]').eqHeights();
$('.foo [class*="eq-"]').eqHeights();
$('.foo2 [class*="eq-"]').eqHeights();
});
use <div class="row foo"> for <div class="row">
and add eq-col-sm-6 eq-col-md-3 class to all your divs with col-sm-6 col-md-3
here DEMO