Save dark mode in php cookies - css

I worked on a simple little code for black mode
I would just like to store the theme for the user in cookies for x time
My Dark Theme in css :
body {
padding: 0px;
color: black;
font-size: 25px;
background-color: white;
}
.dark-mode .mainSection {
background-color: black;
color: white;
padding: 600px;
padding-top: 470px;
}
Html
<div id="dark-mode">
<li><font size="2">
<a class="nav-link" onclick="myFunction()">Dark Mode</font
</li>
</div>
Javascript :
function myFunction() {
var element = document.body;
element.classList.toggle("dark-mode");
}
Thank you

Simply set the cookie using javascript, and the next time the client makes the request to server, the cookies will be send along with it. The below code should work
function createCookie(name, value, timeInSeconds) {
var date = new Date();
date.setTime(date.getTime()+(timeInSeconds*1000));
var expires = "; expires="+date.toGMTString();
document.cookie = name+"="+value+expires+"; path=/";
}
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function myFunction() {
var element = document.body;
const isDarkModeOn = element.classList.toggle("dark-mode");
createCookie("isDarkModeOn", isDarkMode.toString(), 60 * 60 * 24); // 1 day expiry date
}
window.onload = function () {
const isDarkModeOn = getCookie("isDarkModeOn");
if(isDarkModeOn === "true") document.body.classList.add("dark-mode");
}
After that simply check the cookie in PHP,
<?php
$isDarkModeOn = $_COOKIE["isDarkModeOn"] === "true";
if($isDarkModeOn) {
echo "Dark Mode is on";
}
?>

Related

lightgallery doesnt work good

My gallery starts from my first img,but at counter its number 2, and when go back to first picture it shows me nothing, just black window (at first it shows me "src undefined", but now just black window). I want my first
picture to be number 1 in counter etc. i am not sure where is the problem, so i gave you js code from lightgallery here
(function() {
'use strict';
var defaults = {
mode: 'lg-slide',
// Ex : 'ease'
cssEasing: 'ease',
//'for jquery animation'
easing: 'linear',
speed: 600,
height: '100%',
width: '100%',
addClass: '',
startClass: 'lg-start-zoom',
backdropDuration: 150,
hideBarsDelay: 6000,
useLeft: false,
closable: true,
loop: true,
escKey: true,
keyPress: true,
controls: true,
slideEndAnimatoin: true,
hideControlOnEnd: false,
mousewheel: true,
getCaptionFromTitleOrAlt: true,
// .lg-item || '.lg-sub-html'
appendSubHtmlTo: '.lg-sub-html',
subHtmlSelectorRelative: false,
/**
* #desc number of preload slides
* will exicute only after the current slide is fully loaded.
*
* #ex you clicked on 4th image and if preload = 1 then 3rd slide and 5th
* slide will be loaded in the background after the 4th slide is fully loaded..
* if preload is 2 then 2nd 3rd 5th 6th slides will be preloaded.. ... ...
*
*/
preload: 0,
showAfterLoad: true,
selector: '',
selectWithin: '',
nextHtml: '',
prevHtml: '',
// 0, 1
index: false,
iframeMaxWidth: '100%',
download: true,
counter: true,
appendCounterTo: '.lg-toolbar',
swipeThreshold: 50,
enableSwipe: true,
enableDrag: true,
dynamic: false,
dynamicEl: [],
galleryId: 1
};
function Plugin(element, options) {
// Current lightGallery element
this.el = element;
// Current jquery element
this.$el = $(element);
// lightGallery settings
this.s = $.extend({}, defaults, options);
// When using dynamic mode, ensure dynamicEl is an array
if (this.s.dynamic && this.s.dynamicEl !== 'undefined' && this.s.dynamicEl.constructor === Array && !this.s.dynamicEl.length) {
throw ('When using dynamic mode, you must also define dynamicEl as an Array.');
}
// lightGallery modules
this.modules = {};
// false when lightgallery complete first slide;
this.lGalleryOn = false;
this.lgBusy = false;
// Timeout function for hiding controls;
this.hideBartimeout = false;
// To determine browser supports for touch events;
this.isTouch = ('ontouchstart' in document.documentElement);
// Disable hideControlOnEnd if sildeEndAnimation is true
if (this.s.slideEndAnimatoin) {
this.s.hideControlOnEnd = false;
}
// Gallery items
if (this.s.dynamic) {
this.$items = this.s.dynamicEl;
} else {
if (this.s.selector === 'this') {
this.$items = this.$el;
} else if (this.s.selector !== '') {
if (this.s.selectWithin) {
this.$items = $(this.s.selectWithin).find(this.s.selector);
} else {
this.$items = this.$el.find($(this.s.selector));
}
} else {
this.$items = this.$el.children();
}
}
// .lg-item
this.$slide = '';
// .lg-outer
this.$outer = '';
this.init();
return this;
}
Plugin.prototype.init = function() {
var _this = this;
// s.preload should not be more than $item.length
if (_this.s.preload > _this.$items.length) {
_this.s.preload = _this.$items.length;
}
// if dynamic option is enabled execute immediately
var _hash = window.location.hash;
if (_hash.indexOf('lg=' + this.s.galleryId) > 0) {
_this.index = parseInt(_hash.split('&slide=')[1], 10);
$('body').addClass('lg-from-hash');
if (!$('body').hasClass('lg-on')) {
setTimeout(function() {
_this.build(_this.index);
});
$('body').addClass('lg-on');
}
}
if (_this.s.dynamic) {
_this.$el.trigger('onBeforeOpen.lg');
_this.index = _this.s.index || 0;
// prevent accidental double execution
if (!$('body').hasClass('lg-on')) {
setTimeout(function() {
_this.build(_this.index);
$('body').addClass('lg-on');
});
}
} else {
// Using different namespace for click because click event should not unbind if selector is same object('this')
_this.$items.on('click.lgcustom', function(event) {
// For IE8
try {
event.preventDefault();
event.preventDefault();
} catch (er) {
event.returnValue = false;
}
_this.$el.trigger('onBeforeOpen.lg');
_this.index = _this.s.index || _this.$items.index(this);
// prevent accidental double execution
if (!$('body').hasClass('lg-on')) {
_this.build(_this.index);
$('body').addClass('lg-on');
}
});
}
};
Plugin.prototype.build = function(index) {
var _this = this;
_this.structure();
// module constructor
$.each($.fn.lightGallery.modules, function(key) {
_this.modules[key] = new $.fn.lightGallery.modules[key](_this.el);
});
// initiate slide function
_this.slide(index, false, false, false);
if (_this.s.keyPress) {
_this.keyPress();
}
if (_this.$items.length > 1) {
_this.arrow();
setTimeout(function() {
_this.enableDrag();
_this.enableSwipe();
}, 50);
if (_this.s.mousewheel) {
_this.mousewheel();
}
} else {
_this.$slide.on('click.lg', function() {
_this.$el.trigger('onSlideClick.lg');
});
}
_this.counter();
_this.closeGallery();
_this.$el.trigger('onAfterOpen.lg');
// Hide controllers if mouse doesn't move for some period
_this.$outer.on('mousemove.lg click.lg touchstart.lg', function() {
_this.$outer.removeClass('lg-hide-items');
clearTimeout(_this.hideBartimeout);
// Timeout will be cleared on each slide movement also
_this.hideBartimeout = setTimeout(function() {
_this.$outer.addClass('lg-hide-items');
}, _this.s.hideBarsDelay);
});
_this.$outer.trigger('mousemove.lg');
};
Plugin.prototype.structure = function() {
var list = '';
var controls = '';
var subHtmlCont = '';
var template;
var _this = this;
$('body').append('<div class="lg-backdrop"></div>');
$('.lg-backdrop').css('transition-duration', this.s.backdropDuration + 'ms');
// Create gallery items
for (var i = 0; i < this.$items.length; i++) {
list += '<div class="lg-item"></div>';
}
Plugin.prototype.isVideo = function(src, index) {
var html;
if (this.s.dynamic) {
html = this.s.dynamicEl[index].html;
} else {
html = this.$items.eq(index).attr('data-html');
}
if (!src) {
if(html) {
return {
html5: true
};
} else {
console.error('lightGallery :- data-src is not pvovided on slide item ' + (index + 1) + '. Please make sure the selector property is properly configured. More info - http://sachinchoolur.github.io/lightGallery/demos/html-markup.html');
return false;
}
}
var youtube = src.match(/\/\/(?:www\.)?youtu(?:\.be|be\.com|be-nocookie\.com)\/(?:watch\?v=|embed\/)?([a-z0-9\-\_\%]+)/i);
var vimeo = src.match(/\/\/(?:www\.)?vimeo.com\/([0-9a-z\-_]+)/i);
var dailymotion = src.match(/\/\/(?:www\.)?dai.ly\/([0-9a-z\-_]+)/i);
var vk = src.match(/\/\/(?:www\.)?(?:vk\.com|vkontakte\.ru)\/(?:video_ext\.php\?)(.*)/i);
if (youtube) {
return {
youtube: youtube
};
} else if (vimeo) {
return {
vimeo: vimeo
};
} else if (dailymotion) {
return {
dailymotion: dailymotion
};
} else if (vk) {
return {
vk: vk
};
}
};
/**
* #desc Create image counter
* Ex: 1/10
*/
Plugin.prototype.counter = function() {
if (this.s.counter) {
$(this.s.appendCounterTo).append('<div id="lg-counter"><span id="lg-counter-current">' + (parseInt(this.index, 10) + 1) + '</span> / <span id="lg-counter-all">' + this.$items.length + '</span></div>');
}
};
/**
* #desc add sub-html into the slide
* #param {Number} index - index of the slide
*/
Plugin.prototype.addHtml = function(index) {
var subHtml = null;
var subHtmlUrl;
var $currentEle;
if (this.s.dynamic) {
if (this.s.dynamicEl[index].subHtmlUrl) {
subHtmlUrl = this.s.dynamicEl[index].subHtmlUrl;
} else {
subHtml = this.s.dynamicEl[index].subHtml;
}
} else {
$currentEle = this.$items.eq(index);
if ($currentEle.attr('data-sub-html-url')) {
subHtmlUrl = $currentEle.attr('data-sub-html-url');
} else {
subHtml = $currentEle.attr('data-sub-html');
if (this.s.getCaptionFromTitleOrAlt && !subHtml) {
subHtml = $currentEle.attr('title') || $currentEle.find('img').first().attr('alt');
}
}
}
if (!subHtmlUrl) {
if (typeof subHtml !== 'undefined' && subHtml !== null) {
// get first letter of subhtml
// if first letter starts with . or # get the html form the jQuery object
var fL = subHtml.substring(0, 1);
if (fL === '.' || fL === '#') {
if (this.s.subHtmlSelectorRelative && !this.s.dynamic) {
subHtml = $currentEle.find(subHtml).html();
} else {
subHtml = $(subHtml).html();
}
}
} else {
subHtml = '';
}
}
if (this.s.appendSubHtmlTo === '.lg-sub-html') {
if (subHtmlUrl) {
this.$outer.find(this.s.appendSubHtmlTo).load(subHtmlUrl);
} else {
this.$outer.find(this.s.appendSubHtmlTo).html(subHtml);
}
} else {
if (subHtmlUrl) {
this.$slide.eq(index).load(subHtmlUrl);
} else {
this.$slide.eq(index).append(subHtml);
}
}
// Add lg-empty-html class if title doesn't exist
if (typeof subHtml !== 'undefined' && subHtml !== null) {
if (subHtml === '') {
this.$outer.find(this.s.appendSubHtmlTo).addClass('lg-empty-html');
} else {
this.$outer.find(this.s.appendSubHtmlTo).removeClass('lg-empty-html');
}
}
this.$el.trigger('onAfterAppendSubHtml.lg', [index]);
};
/**
* #desc Preload slides
* #param {Number} index - index of the slide
*/
Plugin.prototype.preload = function(index) {
var i = 1;
var j = 1;
for (i = 1; i <= this.s.preload; i++) {
if (i >= this.$items.length - index) {
break;
}
this.loadContent(index + i, false, 0);
}
for (j = 1; j <= this.s.preload; j++) {
if (index - j < 0) {
break;
}
this.loadContent(index - j, false, 0);
}
};
/**
* #desc Load slide content into slide.
* #param {Number} index - index of the slide.
* #param {Boolean} rec - if true call loadcontent() function again.
* #param {Boolean} delay - delay for adding complete class. it is 0 except first time.
*/
Plugin.prototype.loadContent = function(index, rec, delay) {
var _this = this;
var _hasPoster = false;
var _$img;
var _src;
var _poster;
var _srcset;
var _sizes;
var _html;
var getResponsiveSrc = function(srcItms) {
var rsWidth = [];
var rsSrc = [];
for (var i = 0; i < srcItms.length; i++) {
var __src = srcItms[i].split(' ');
// Manage empty space
if (__src[0] === '') {
__src.splice(0, 1);
}
rsSrc.push(__src[0]);
rsWidth.push(__src[1]);
}
var wWidth = $(window).width();
for (var j = 0; j < rsWidth.length; j++) {
if (parseInt(rsWidth[j], 10) > wWidth) {
_src = rsSrc[j];
break;
}
}
};
if (_this.s.dynamic) {
if (_this.s.dynamicEl[index].poster) {
_hasPoster = true;
_poster = _this.s.dynamicEl[index].poster;
}
_html = _this.s.dynamicEl[index].html;
_src = _this.s.dynamicEl[index].src;
if (_this.s.dynamicEl[index].responsive) {
var srcDyItms = _this.s.dynamicEl[index].responsive.split(',');
getResponsiveSrc(srcDyItms);
}
_srcset = _this.s.dynamicEl[index].srcset;
_sizes = _this.s.dynamicEl[index].sizes;
} else {
if (_this.$items.eq(index).attr('data-poster')) {
_hasPoster = true;
_poster = _this.$items.eq(index).attr('data-poster');
}
_html = _this.$items.eq(index).attr('data-html');
_src = _this.$items.eq(index).attr('href') || _this.$items.eq(index).attr('data-src');
if (_this.$items.eq(index).attr('data-responsive')) {
var srcItms = _this.$items.eq(index).attr('data-responsive').split(',');
getResponsiveSrc(srcItms);
}
_srcset = _this.$items.eq(index).attr('data-srcset');
_sizes = _this.$items.eq(index).attr('data-sizes');
}
if (_src || _srcset || _sizes || _poster) {
var iframe = false;
if (_this.s.dynamic) {
if (_this.s.dynamicEl[index].iframe) {
iframe = true;
}
} else {
if (_this.$items.eq(index).attr('data-iframe') === 'true') {
iframe = true;
}
}
var _isVideo = _this.isVideo(_src, index);
if (!_this.$slide.eq(index).hasClass('lg-loaded')) {
if (iframe) {
_this.$slide.eq(index).prepend('<div class="lg-video-cont lg-has-iframe" style="max-width:' + _this.s.iframeMaxWidth + '"><div class="lg-video"><iframe class="lg-object" frameborder="0" src="' + _src + '" allowfullscreen="true"></iframe></div></div>');
} else if (_hasPoster) {
var videoClass = '';
if (_isVideo && _isVideo.youtube) {
videoClass = 'lg-has-youtube';
} else if (_isVideo && _isVideo.vimeo) {
videoClass = 'lg-has-vimeo';
} else {
videoClass = 'lg-has-html5';
}
_this.$slide.eq(index).prepend('<div class="lg-video-cont ' + videoClass + ' "><div class="lg-video"><span class="lg-video-play"></span><img class="lg-object lg-has-poster" src="' + _poster + '" /></div></div>');
} else if (_isVideo) {
_this.$slide.eq(index).prepend('<div class="lg-video-cont "><div class="lg-video"></div></div>');
_this.$el.trigger('hasVideo.lg', [index, _src, _html]);
} else {
_this.$slide.eq(index).prepend('<div class="lg-img-wrap"><img class="lg-object lg-image" src="' + _src + '" /></div>');
}
_this.$el.trigger('onAferAppendSlide.lg', [index]);
_$img = _this.$slide.eq(index).find('.lg-object');
if (_sizes) {
_$img.attr('sizes', _sizes);
}
if (_srcset) {
_$img.attr('srcset', _srcset);
try {
picturefill({
elements: [_$img[0]]
});
} catch (e) {
console.warn('lightGallery :- If you want srcset to be supported for older browser please include picturefil version 2 javascript library in your document.');
}
}
if (this.s.appendSubHtmlTo !== '.lg-sub-html') {
_this.addHtml(index);
}
_this.$slide.eq(index).addClass('lg-loaded');
}
_this.$slide.eq(index).find('.lg-object').on('load.lg error.lg', function() {
// For first time add some delay for displaying the start animation.
var _speed = 0;
// Do not change the delay value because it is required for zoom plugin.
// If gallery opened from direct url (hash) speed value should be 0
if (delay && !$('body').hasClass('lg-from-hash')) {
_speed = delay;
}
setTimeout(function() {
_this.$slide.eq(index).addClass('lg-complete');
_this.$el.trigger('onSlideItemLoad.lg', [index, delay || 0]);
}, _speed);
});
// #todo check load state for html5 videos
if (_isVideo && _isVideo.html5 && !_hasPoster) {
_this.$slide.eq(index).addClass('lg-complete');
}
if (rec === true) {
if (!_this.$slide.eq(index).hasClass('lg-complete')) {
_this.$slide.eq(index).find('.lg-object').on('load.lg error.lg', function() {
_this.preload(index);
});
} else {
_this.preload(index);
}
}
}
};
Plugin.prototype.slide = function(index, fromTouch, fromThumb, direction) {
var _prevIndex = this.$outer.find('.lg-current').index();
var _this = this;
// Prevent if multiple call
// Required for hsh plugin
if (_this.lGalleryOn && (_prevIndex === index)) {
return;
}
var _length = this.$slide.length;
var _time = _this.lGalleryOn ? this.s.speed : 0;
if (!_this.lgBusy) {
if (this.s.download) {
var _src;
if (_this.s.dynamic) {
_src = _this.s.dynamicEl[index].downloadUrl !== false && (_this.s.dynamicEl[index].downloadUrl || _this.s.dynamicEl[index].src);
} else {
_src = _this.$items.eq(index).attr('data-download-url') !== 'false' && (_this.$items.eq(index).attr('data-download-url') || _this.$items.eq(index).attr('href') || _this.$items.eq(index).attr('data-src'));
}
if (_src) {
$('#lg-download').attr('href', _src);
_this.$outer.removeClass('lg-hide-download');
} else {
_this.$outer.addClass('lg-hide-download');
}
}
this.$el.trigger('onBeforeSlide.lg', [_prevIndex, index, fromTouch, fromThumb]);
_this.lgBusy = true;
clearTimeout(_this.hideBartimeout);
// Add title if this.s.appendSubHtmlTo === lg-sub-html
if (this.s.appendSubHtmlTo === '.lg-sub-html') {
// wait for slide animation to complete
setTimeout(function() {
_this.addHtml(index);
}, _time);
}
this.arrowDisable(index);
if (!direction) {
if (index < _prevIndex) {
direction = 'prev';
} else if (index > _prevIndex) {
direction = 'next';
}
}
if (!fromTouch) {
// remove all transitions
_this.$outer.addClass('lg-no-trans');
this.$slide.removeClass('lg-prev-slide lg-next-slide');
if (direction === 'prev') {
//prevslide
this.$slide.eq(index).addClass('lg-prev-slide');
this.$slide.eq(_prevIndex).addClass('lg-next-slide');
} else {
// next slide
this.$slide.eq(index).addClass('lg-next-slide');
this.$slide.eq(_prevIndex).addClass('lg-prev-slide');
}
// give 50 ms for browser to add/remove class
setTimeout(function() {
_this.$slide.removeClass('lg-current');
//_this.$slide.eq(_prevIndex).removeClass('lg-current');
_this.$slide.eq(index).addClass('lg-current');
// reset all transitions
_this.$outer.removeClass('lg-no-trans');
}, 50);
} else {
this.$slide.removeClass('lg-prev-slide lg-current lg-next-slide');
var touchPrev;
var touchNext;
if (_length > 2) {
touchPrev = index - 1;
touchNext = index + 1;
if ((index === 0) && (_prevIndex === _length - 1)) {
// next slide
touchNext = 0;
touchPrev = _length - 1;
} else if ((index === _length - 1) && (_prevIndex === 0)) {
// prev slide
touchNext = 0;
touchPrev = _length - 1;
}
} else {
touchPrev = 0;
touchNext = 1;
}
if (direction === 'prev') {
_this.$slide.eq(touchNext).addClass('lg-next-slide');
} else {
_this.$slide.eq(touchPrev).addClass('lg-prev-slide');
}
_this.$slide.eq(index).addClass('lg-current');
}
if (_this.lGalleryOn) {
setTimeout(function() {
_this.loadContent(index, true, 0);
}, this.s.speed + 50);
setTimeout(function() {
_this.lgBusy = false;
_this.$el.trigger('onAfterSlide.lg', [_prevIndex, index, fromTouch, fromThumb]);
}, this.s.speed);
} else {
_this.loadContent(index, true, _this.s.backdropDuration);
_this.lgBusy = false;
_this.$el.trigger('onAfterSlide.lg', [_prevIndex, index, fromTouch, fromThumb]);
}
_this.lGalleryOn = true;
if (this.s.counter) {
$('#lg-counter-current').text(index + 1);
}
}
_this.index = index;
};
$(document).ready(function() {
for(var i=0; i<10 ; i++){
$('#lightgallery' + i).lightGallery({
pager: true
});
}
});
HTML:
<section id="galler" style="display: flex; flex-wrap: wrap; justify-content:center;">
<div class="demo-gallery">
<ul id="lightgallery0">
<h4>First love 29.07.2018.</h4>
<li data-responsive="img/29.07.2018/1.jpg 375, img/29.07.2018/1.jpg 480, img/29.07.2018/1.jpg 800" data-src="img/29.07.2018/1.jpg" data-sub-html="<h4>Solar Matinee - First Love</h4> " style='display:inline-block;' data-pinterest-text="Pin it">
<a href="">
<img class="img-responsive" src="img/29.07.2018/1.jpg">
<div class="demo-gallery-poster">
<img src="img/zoom.png">
</div>
</a>
</li>
<li data-responsive="img/29.07.2018/2.jpg 375, img/29.07.2018/2.jpg 800" class="none" data-src="img/29.07.2018/2.jpg" data-pinterest-text="Pin it">
</li>
<li data-responsive="img/29.07.2018/3.jpg 375, img/29.07.2018/3.jpg 800" class="none" data-src="img/29.07.2018/3.jpg" data-pinterest-text="Pin it">
</li>
</ul>
</div>
</section>
Your js script is missing some lines, to minimize the error possibility I called them over CDN and made little changes on your script. Have a look at snippet:
$(document).ready(function() {
$("[id^=lightgallery]").lightGallery({ /* no need to loop, just select elements which starting with "lightgallery" */
pager: true,
selector: 'li' /* we need to show which elements are our items (to not assume h4 as a slider item [reason of "data-src is not pvovided on slide item 1" error]) */
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/lightgallery/1.6.11/css/lightgallery.min.css" rel="stylesheet">
<style type="text/css">
body {
background-color: #152836
}
.demo-gallery>ul {
margin-bottom: 0;
}
.demo-gallery>ul>li {
float: left;
margin-bottom: 15px;
margin-right: 20px;
width: 200px;
}
.demo-gallery>ul>li a {
border: 3px solid #FFF;
border-radius: 3px;
display: block;
overflow: hidden;
position: relative;
float: left;
}
.demo-gallery>ul>li a>img {
-webkit-transition: -webkit-transform 0.15s ease 0s;
-moz-transition: -moz-transform 0.15s ease 0s;
-o-transition: -o-transform 0.15s ease 0s;
transition: transform 0.15s ease 0s;
-webkit-transform: scale3d(1, 1, 1);
transform: scale3d(1, 1, 1);
height: 100%;
width: 100%;
}
.demo-gallery>ul>li a:hover>img {
-webkit-transform: scale3d(1.1, 1.1, 1.1);
transform: scale3d(1.1, 1.1, 1.1);
}
.demo-gallery>ul>li a:hover .demo-gallery-poster>img {
opacity: 1;
}
.demo-gallery>ul>li a .demo-gallery-poster {
background-color: rgba(0, 0, 0, 0.1);
bottom: 0;
left: 0;
position: absolute;
right: 0;
top: 0;
-webkit-transition: background-color 0.15s ease 0s;
-o-transition: background-color 0.15s ease 0s;
transition: background-color 0.15s ease 0s;
}
.demo-gallery>ul>li a .demo-gallery-poster>img {
left: 50%;
margin-left: -10px;
margin-top: -10px;
opacity: 0;
position: absolute;
top: 50%;
-webkit-transition: opacity 0.3s ease 0s;
-o-transition: opacity 0.3s ease 0s;
transition: opacity 0.3s ease 0s;
}
.demo-gallery>ul>li a:hover .demo-gallery-poster {
background-color: rgba(0, 0, 0, 0.5);
}
.demo-gallery .justified-gallery>a>img {
-webkit-transition: -webkit-transform 0.15s ease 0s;
-moz-transition: -moz-transform 0.15s ease 0s;
-o-transition: -o-transform 0.15s ease 0s;
transition: transform 0.15s ease 0s;
-webkit-transform: scale3d(1, 1, 1);
transform: scale3d(1, 1, 1);
height: 100%;
width: 100%;
}
.demo-gallery .justified-gallery>a:hover>img {
-webkit-transform: scale3d(1.1, 1.1, 1.1);
transform: scale3d(1.1, 1.1, 1.1);
}
.demo-gallery .justified-gallery>a:hover .demo-gallery-poster>img {
opacity: 1;
}
.demo-gallery .justified-gallery>a .demo-gallery-poster {
background-color: rgba(0, 0, 0, 0.1);
bottom: 0;
left: 0;
position: absolute;
right: 0;
top: 0;
-webkit-transition: background-color 0.15s ease 0s;
-o-transition: background-color 0.15s ease 0s;
transition: background-color 0.15s ease 0s;
}
.demo-gallery .justified-gallery>a .demo-gallery-poster>img {
left: 50%;
margin-left: -10px;
margin-top: -10px;
opacity: 0;
position: absolute;
top: 50%;
-webkit-transition: opacity 0.3s ease 0s;
-o-transition: opacity 0.3s ease 0s;
transition: opacity 0.3s ease 0s;
}
.demo-gallery .justified-gallery>a:hover .demo-gallery-poster {
background-color: rgba(0, 0, 0, 0.5);
}
.demo-gallery .video .demo-gallery-poster img {
height: 48px;
margin-left: -24px;
margin-top: -24px;
opacity: 0.8;
width: 48px;
}
.demo-gallery.dark>ul>li a {
border: 3px solid #04070a;
}
.home .demo-gallery {
padding-bottom: 80px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body class="home">
<div class="demo-gallery">
<ul id="lightgallery0">
<h4>First love 29.07.2018.</h4>
<li data-src="https://dummyimage.com/600x400/000000/fff&text=1" data-sub-html="<h4>Solar Matinee - First Love</h4> " style='display:inline-block;' data-pinterest-text="Pin it">
<a href="">
<img class="img-responsive" src="https://dummyimage.com/600x400/000000/fff&text=1">
</a>
</li>
<li class="none" data-src="https://dummyimage.com/600x400/000000/fff&text=2" data-pinterest-text="Pin it">
<a href="">
<img class="img-responsive" src="https://dummyimage.com/600x400/000000/fff&text=2">
</a>
</li>
<li class="none" data-src="https://dummyimage.com/600x400/000000/fff&text=3" data-pinterest-text="Pin it">
<a href="">
<img class="img-responsive" src="https://dummyimage.com/600x400/000000/fff&text=3">
</a>
</li>
</ul>
</div>
<script src="https://cdn.jsdelivr.net/picturefill/2.3.1/picturefill.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lightgallery/1.6.11/js/lightgallery-all.js"></script>
</body>
</html>

Custom cursor not supported in Edge?

if(!CSS.supports('cursor', 'url(cursor.png), pointer')) {
var myCursor = document.createElement('img');
myCursor.src = 'cursor.png';
myCursor.style.position = 'absolute';
document.body.appendChild(myCursor);
document.addEventListener('mousemove', function(e) {
myCursor.style.left = e.pageX+'px';
myCursor.style.top = e.pageY+'px';
}, false);
}
body{
padding:0;
margin:0;
background-color: #19321D;
color: #53CC66;
line-height: 1.5;
font-family: FreeMono, monospace;
cursor: url(cursor.png), pointer;
}
a{
text-decoration: none;
color: #53CC66;
}
ul{
text-decoration: none;
list-style-type: none;
}
#header{
text-align: center;
border-bottom: 3px solid #53CC66;
margin-bottom: 100px;
width: 90%;
margin-left: auto;
margin-right: auto;
margin-top: 25px;
line-height: 1;
}
h1, h2, h3{
color: #53CC66;
font-family: FreeMono, monospace;
font-size: 15px;
}
a{
cursor: url(cursor.png), pointer;
}
a:hover {
cursor: url(cursor.png), pointer;
color: #19321D;
}
li:hover{
background-color:#53CC66;
color: #19321D;
}
li:hover a{
color: #19321D;
}
<html>
<head>
<title>Getrate|Command promph </title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<link rel="stylesheet" href="styles15.css" type="text/css" />
</head>
<body>
<div id="wrapper">
<div id="header">
<h1>DAVID SECRET INDUSTRIES UNVERIFIED SYSTEM</h1>
<h2>COPYRIGHT 2015 - 2050 ALL RIGHT RESERVED</h2>
<h3>- SERVER #1 -</h3>
</div>
<ul>
<li>[CONZOLE] > -TOP SECRET- . PAGE //stripslash 1.3.8.9.84.113.21.73</li>
<li>[CONZOLE] > -TOP SECRET- . PAGE //stripslash 1.4.8.9.84.113.21.74</li>
<li>[CONZOLE] > -TOP SECRET- . PAGE //stripslash 1.5.8.9.84.113.21.75</li>
<li>[CONZOLE] > -TOP SECRET- . PAGE //stripslash 1.6.8.9.84.113.21.76</li>
<li>[CONZOLE] > -TOP SECRET- . PAGE //stripslash 1.7.8.9.84.113.21.77</li>
</ul>
</div>
</body>
<script src="wow.js"></script>
</html>
I just thought, is there any possible way, to make custom cursor, that works on microsoft edge? On my website, i used this:
body{ cursor: url(cursor.png), pointer;}
but in microsoft edge, it is not working...
Any ideas how to solve this?/Is there any other way?
So.... after small recode, my website looks like this, see the fiddle and try, it is not working yet...
This property is not supported yet : http://caniuse.com/#search=cursor
This property is now supported : caniuse.com:cursor:url()
As Charaf mentioned: the property isn't yet supported in Edge. If your project requires a solution, you can sort of mimic the behavior with JavaScript.
JavaScript:
if(!CSS.supports('cursor', 'url(cursor.png), pointer')) {
var myCursor = document.createElement('img');
myCursor.src = 'cursor.png';
myCursor.style.position = 'absolute';
document.body.appendChild(myCursor);
document.addEventListener('mousemove', function(e) {
myCursor.style.left = e.pageX+'px';
myCursor.style.top = e.pageY+'px';
}, false);
}
I made a library called CursorJS for you. You can check it out here. If you scroll to the bottom of the JavaScript code, you can find initializing code:
/* Enable lib with cursor image src */
CursorJS.enable('http://files.softicons.com/download/toolbar-icons/plastic-mini-icons-by-deleket/png/32x32/Cursor-01.png');
CursorJS.addEl(document.querySelector('.myElement1'));
CursorJS.addEl(document.querySelector('.myElement3'));
In your case just do the following:
/* Enable lib with cursor image src */
CursorJS.enable('./cursor.png');
CursorJS.addEl(document.body);
Customization
CursorJS has a mouseOffset variable. It repesents difference of mouse position and position of image. For example, if I set it to
mouseOffset: {
x: 50,
y: 50
},
The mouse will be 50px off. The reason why I made this variable is that custom mouse was kind of "blinking", try to set it to {x:1,y:1} ;)
Live example
var CursorJS = {
img: new Image(),
els: [],
mouseOffset: {
x: 5,
y: 5
},
addedImg: false,
checkForIE: function() {
return (/MSIE/i.test(navigator.userAgent)
|| /rv:11.0/i.test(navigator.userAgent));
},
setDisplay: function() {
this.img.style.display =
this.els.indexOf(true) > -1 ? null : 'none';
},
getMouseCoords: function(e) {
var mx = 0, my = 0;
if (this.checkForIE())
mx = event.clientX + document.body.scrollLeft,
my = event.clientY + document.body.scrollTop;
else
mx = e.pageX,my = e.pageY;
if (mx < 0) mx = 0;
if (my < 0) my = 0;
return [mx, my];
},
mouseOver: function(e, id) {
this.els[id] = true;
this.setDisplay();
var coords = this.getMouseCoords(e);
this.img.style.left =
(coords[0]+this.mouseOffset.x) + 'px';
this.img.style.top =
(coords[1]+this.mouseOffset.y) + 'px';
},
mouseOut: function(e, id) {
this.els[id] = false;
this.setDisplay();
},
mouseMove: function(e) {
var coords = this.getMouseCoords(e);
this.img.style.left =
(coords[0]+this.mouseOffset.x) + 'px';
this.img.style.top =
(coords[1]+this.mouseOffset.y) + 'px';
},
addEvent: function(el, name, func, bool) {
if (el == null || typeof name != 'string'
|| typeof func != 'function'
|| typeof bool != 'boolean')
return;
if (el.addEventListener)
el.addEventListener(name, func, false);
else if (el.attachEvent)
el.attachEvent('on' + name, func);
else
el['on' + name] = func;
},
addEl: function(el) {
var evts = ['over','out','move'],
id = this.els.length;
this.els.push(false);
this.el = el;
this.addEvent(el, 'mouseover', function(e) {
this.mouseOver(e, id) }.bind(this), false);
this.addEvent(el, 'mouseout', function(e) {
this.mouseOut(e, id) }.bind(this), false);
this.addEvent(el, 'mousemove', function(e) {
this.mouseMove(e) }.bind(this), false);
if (typeof el['style'] != 'undefined')
el.style.cursor = 'none';
},
enable: function(src) {
this.img.src = src;
this.img.style.display = 'none';
this.img.style.position = 'absolute';
this.img.style.cursor = 'none';
this.addEvent(this.img, 'mousemove', function(e) {
this.mouseMove(e) }.bind(this), false);
if (!this.addedImg)
document.body.appendChild(this.img),
this.addedImg = true;
}
}
/*** INITIALIZE ***/
CursorJS.enable('http://files.softicons.com/download/toolbar-icons/plastic-mini-icons-by-deleket/png/32x32/Cursor-01.png');
CursorJS.addEl(document.querySelector('.myElement1'));
CursorJS.addEl(document.querySelector('.myElement3'));
.myElement1, .myElement2, .myElement3 {
width: 150px;
height: 150px;
border: 1px solid gray;
display: inline-block;
}
<div class="myElement1">added</div>
<div class="myElement2">not added</div>
<div class="myElement3">added</div>
Hope that worked! Have a nice day :)

Set animated html5 canvas as the background without interacting with other elements?

I got the canvas working, I'm having issues trying to position it.
Specifically I want to implement them to the same effect as:
html {
background: url(back.jpg) no-repeat center center fixed;
background-size: cover;
}
for static images. Basically no interaction with other elements, and positioned as low as possible with regards to the stacking context. Additionally, I'd like to have the canvas background as compartmentalized / as segmented as possible from the rest of the code.
By segmented, I mean something like this:
<body>
<div id="backgroundContainer">
<canvas id="myCanvas"></canvas>
</div>
<div id="everythingElseContainer">
....
</div>
<script src="canvasAnimation.js"></script>
</body>
or this:
<body>
<div id="container">
<canvas id="myCanvas"></canvas>
<div id="everythingElse">
....
</div>
</div>
<script src="canvasAnimation.js"></script>
</body>
to minimize the possibility of css conflicts.
var WIDTH;
var HEIGHT;
var canvas;
var con;
var g;
var pxs = new Array();
var rint = 60;
$(document).ready(function(){
WIDTH = window.innerWidth;
HEIGHT = window.innerHeight;
canvas = document.getElementById('canvas');
$(canvas).attr('width', WIDTH).attr('height',HEIGHT);
con = canvas.getContext('2d');
for(var i = 0; i < 100; i++) {
pxs[i] = new Circle();
pxs[i].reset();
}
setInterval(draw,rint);
});
function draw() {
con.clearRect(0,0,WIDTH,HEIGHT);
for(var i = 0; i < pxs.length; i++) {
pxs[i].fade();
pxs[i].move();
pxs[i].draw();
}
}
function Circle() {
this.s = {ttl:8000, xmax:5, ymax:2, rmax:10, rt:1, xdef:960, ydef:540, xdrift:4, ydrift: 4, random:true, blink:true};
this.reset = function() {
this.x = (this.s.random ? WIDTH*Math.random() : this.s.xdef);
this.y = (this.s.random ? HEIGHT*Math.random() : this.s.ydef);
this.r = ((this.s.rmax-1)*Math.random()) + 1;
this.dx = (Math.random()*this.s.xmax) * (Math.random() < .5 ? -1 : 1);
this.dy = (Math.random()*this.s.ymax) * (Math.random() < .5 ? -1 : 1);
this.hl = (this.s.ttl/rint)*(this.r/this.s.rmax);
this.rt = Math.random()*this.hl;
this.s.rt = Math.random()+1;
this.stop = Math.random()*.2+.4;
this.s.xdrift *= Math.random() * (Math.random() < .5 ? -1 : 1);
this.s.ydrift *= Math.random() * (Math.random() < .5 ? -1 : 1);
}
this.fade = function() {
this.rt += this.s.rt;
}
this.draw = function() {
if(this.s.blink && (this.rt <= 0 || this.rt >= this.hl)) this.s.rt = this.s.rt*-1;
else if(this.rt >= this.hl) this.reset();
var newo = 1-(this.rt/this.hl);
con.beginPath();
con.arc(this.x,this.y,this.r,0,Math.PI*2,true);
con.closePath();
var cr = this.r*newo;
g = con.createRadialGradient(this.x,this.y,0,this.x,this.y,(cr <= 0 ? 1 : cr));
g.addColorStop(0.0, 'rgba(255,255,255,'+newo+')');
g.addColorStop(this.stop, 'rgba(77,101,181,'+(newo*.6)+')');
g.addColorStop(1.0, 'rgba(77,101,181,0)');
con.fillStyle = g;
con.fill();
}
this.move = function() {
this.x += (this.rt/this.hl)*this.dx;
this.y += (this.rt/this.hl)*this.dy;
if(this.x > WIDTH || this.x < 0) this.dx *= -1;
if(this.y > HEIGHT || this.y < 0) this.dy *= -1;
}
this.getX = function() { return this.x; }
this.getY = function() { return this.y; }
}
html, body, div, button, canvas, .containr {
padding: 0;
border: none;
margin: 0;
}
html, body, .containr{
height: 100%;
width: 100%;
background: none;
}
html, body {
font-size: 13px;
text-decoration: none;
font-family: Verdana, Geneva, sans-serif !important;
}
button {
transition: all 0.24s ease;
}
h1 {
font-size: 4rem;
}
button {
font-size: 5.6rem;
}
#pixie {
position:fixed;
z-index: 0;
background: black;
}
.containr>div {
background: blue;
}
.containr {
overflow:hidden;
color: #ffffff;
z-index: 9;
font-size: 256%;
white-space: nowrap;
display: flex;
flex-flow: column nowrap;
justify-content: space-around;
align-items: center;
align-content: center;
}
.btnz {
margin-left: 2.4%;
margin-right: 2.4%;
background: #ffffff;
background: rgba(0, 0, 0, .36);
text-shadow: 1px 1px 3px #000;
padding: 2rem;
}
.btnz:hover {
background: #3cb0fd;
text-shadow: none;
text-decoration: none;
}
/* Outline Out */
.hvr {
display: inline-block;
vertical-align: middle;
-webkit-transform: translateZ(0);
transform: translateZ(0);
box-shadow: 0 0 1px rgba(0, 0, 0, 0);
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-moz-osx-font-smoothing: grayscale;
position: relative;
}
.hvr:before {
content: '';
position: absolute;
border: #e1e1e1 solid 5px;
top: -4px;
right: -4px;
bottom: -4px;
left: -4px;
-webkit-transition-duration: 0.3s;
transition-duration: 0.3s;
-webkit-transition-property: top, right, bottom, left;
transition-property: top, right, bottom, left;
}
.hvr:hover:before, .hvr:focus:before, .hvr:active:before {
top: -18px;
right: -18px;
bottom: -18px;
left: -18px;
border: #ffffff solid 8px;
}
<!doctype html>
<html lang="en">
<head datetime="2015-10-31">
<link rel="stylesheet" href="main.css">
</head>
<body>
<div class="containr">
<canvas id="canvas"></canvas>
<div>
<h1>Main Title</h1>
</div>
<div>
<button class="btnz hvr">
Button Butt
</button>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="js.js"></script>
</body>
</html>
To move objects down in the visual order use the CSS styling z-index smaller numbers move the element down under other elements, higher numbers bring it up.See MDN z-index for more info.
To set the background of an element to a canvas use
element.style.background= "url(" + canvas.toDataURL() + ")";
To isolate of compartmentalize some code the easiest way is to wrap it in a anonymous function and call it. Everything inside it is isolated. Use 'use strict' directive to ensure you do not accidentally create global scoped variables.
A normal anonymous function does nothing and can not be used.
function(){ console.log(42); }; // does nothing
But if you wrap it in () and then add the function call tokens to the end ( ) you can call it like any function.
(function(){ console.log(42); })(); // send the meaning of life,
// the universe, and everything
// to the console.
The function below wraps up a and nothing can get access to a outside the anonymous function.
(function(){
var a = 1;
})();
But you can easily forget to put var in front of a variable making the variable visible to the entire page.
(function(){
var a = 1;
outThere = 2; // Oh no this is has been placed in
// global scope because it is missing
// the var token.
})();
To stop this use the 'use strict' directive.
(function(){
"use strict"; // this must be the very first line of the function
var a = 1;
outThere = 2; // this will cause the javascript to throw a
// ReferenceError: outThere is not defined
})();
It throws an error and stop the function from running but at least you will know that you have a leak.
Everything inside the anonymous function will manage itself. Deleting itself when not needed any more. Or remaining in memory if the Javascript engine holds an internal reference.
The next function starts up and calls its own function doSomething then exits and is deleted completely including the big array.
(function(){
var bigArray = new Array(100000000);
function doSomething(){
console.log("Whats up?");
}
doSomething();
})();
The next one will create a big array and hold that array in memory for 10 seconds (lifeTime). This is because the setTimeout has given the javascript engine an internal reference to doSomething. As long as that reference exists the bigArray will remain (because of closure). After the timeout the reference his no longer need and thus disposed causing all associated referances to go as well and thus disappear. All done via the magic of garbage collection.
Info on Clouser
Info on Garbage collection MDN is out of date but I am sure a quick search on StackOverflow will help.
(function(){
var bigArray = new Array(100000000);
function doSomething(){
console.log("Big Array has had its time.");
}
setTimeout(doSomething,10000);
})();
Attaching an object to items outside the anonymous function scope will expose data in that object to the global scope.
The next function adds a property to a DOM element. This is visible to the global scope and also means that the lifetime of the function will be as long as that element exists.
(function(){
function Info(){
... create info ..
}
var element = document.getElementById("thisOutsideWorld");
var importantPrivateInfo = new Info();
element.keepThis = importantPrivateInfo;
})();
But this does not apply to primitive types as they are copied not referenced. These are Numbers, Strings, Booleans , Undefined, Null...
So to set the background to a canvas via a compartmentalized function see the following function
(function(){
'use strict';
var myCanvas = document.createElement("canvas");
myCanvas .width = 1024;
myCanvas .height =1024;
var ctx = canvas.getContext("2d");
// toDo
// draw the stuff you want.
var el = document.getElementById("myElement");
if(el !== null){
el.style.background = "url("+canvas.toDataURL()+")";
}
// all done
})(); // once run it will delete the canvas and ctx and leave only the copied dataURL
You may think that this exposes the canvas. But it is safe as the canvas is converted to a string and strings are copied not referenced.
If you need to keep the canvas for some period then use a timer to create an internal reference to the anonymous function
The following function will create a canvas and update it every second for 100 seconds. After that it will be deleted and completely gone.
(function(){
'use strict';
var myCanvas = document.createElement("canvas");
myCanvas .width = 1024;
myCanvas .height =1024;
var lifeCounter = 0;
var ctx = canvas.getContext("2d");
// toDo
// draw the stuff you want.
var el = document.getElementById("myElement");
function update(){
// draw stuff on the canvas
if(el !== null){
el.style.background = "url("+canvas.toDataURL()+")";
}
lifeCounter += 1;
if(lifeCounter < 100){
setTimeout(update,1000);
}
}
update(); //start the updates
// all done
})();
Hope this helps.

Google Maps Api v3: How to change the Default waypoint markers in the Directions (set)Panel?

How do you change the markers with custom markers active in the map in the directions panel and change the color aswell from the body? Any help much appreciated.
The screenshot:
http://i.stack.imgur.com/wYFoc.png
Just to keep alive #MrUpsidown comment and code alive:
Short answer: you can't. Longer answer: don't use the directions
panel. Create your own, with the information you need (from the
directions service response) and add your custom markers.
Nov 24 at 16:32
Fiddle
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var routeBounds = false;
var overlayWidth = 200; // Width of the overlay DIV
var leftMargin = 30; // Grace margin to avoid too close fits on the edge of the overlay
var rightMargin = 80; // Grace margin to avoid too close fits on the right and leave space for the controls
overlayWidth += leftMargin;
var start = new google.maps.LatLng(3.148173, 101.7148792);
var end = new google.maps.LatLng(3.1347725, 101.6893408);
function initialize() {
var btn1 = document.getElementById('calcRoute');
btn1.addEventListener('click', calcRoute);
var btn2 = document.getElementById('offsetMap');
btn2.addEventListener('click', offsetMap);
var btn3 = document.getElementById('fitAndOffsetMap');
btn3.addEventListener('click', fitAndOffsetMap);
var btn4 = document.getElementById('fitMap');
btn4.addEventListener('click', fitMap);
directionsDisplay = new google.maps.DirectionsRenderer({
draggable: true
});
var mapOptions = {
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: start,
panControlOptions: {
position: google.maps.ControlPosition.TOP_RIGHT
},
zoomControlOptions: {
position: google.maps.ControlPosition.TOP_RIGHT
}
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
directionsDisplay.setMap(map);
}
function offsetMap() {
if (routeBounds !== false) {
// Clear listener defined in directions results
google.maps.event.clearListeners(map, 'idle');
// Top right corner
var topRightCorner = new google.maps.LatLng(map.getBounds().getNorthEast().lat(), map.getBounds().getNorthEast().lng());
// Top right point
var topRightPoint = fromLatLngToPoint(topRightCorner).x;
// Get pixel position of leftmost and rightmost points
var leftCoords = routeBounds.getSouthWest();
var leftMost = fromLatLngToPoint(leftCoords).x;
var rightMost = fromLatLngToPoint(routeBounds.getNorthEast()).x;
// Calculate left and right offsets
var leftOffset = (overlayWidth - leftMost);
var rightOffset = ((topRightPoint - rightMargin) - rightMost);
// Only if left offset is needed
if (leftOffset >= 0) {
if (leftOffset < rightOffset) {
var mapOffset = Math.round((rightOffset - leftOffset) / 2);
// Pan the map by the offset calculated on the x axis
map.panBy(-mapOffset, 0);
// Get the new left point after pan
var newLeftPoint = fromLatLngToPoint(leftCoords).x;
if (newLeftPoint <= overlayWidth) {
// Leftmost point is still under the overlay
// Offset map again
offsetMap();
}
} else {
// Cannot offset map at this zoom level otherwise both leftmost and rightmost points will not fit
// Zoom out and offset map again
map.setZoom(map.getZoom() - 1);
offsetMap();
}
}
}
}
function fromLatLngToPoint(latLng) {
var scale = Math.pow(2, map.getZoom());
var nw = new google.maps.LatLng(map.getBounds().getNorthEast().lat(), map.getBounds().getSouthWest().lng());
var worldCoordinateNW = map.getProjection().fromLatLngToPoint(nw);
var worldCoordinate = map.getProjection().fromLatLngToPoint(latLng);
return new google.maps.Point(Math.floor((worldCoordinate.x - worldCoordinateNW.x) * scale), Math.floor((worldCoordinate.y - worldCoordinateNW.y) * scale));
}
function calcRoute() {
var request = {
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
// Define route bounds for use in offsetMap function
routeBounds = response.routes[0].bounds;
// Write directions steps
writeDirectionsSteps(response.routes[0].legs[0].steps);
// Wait for map to be idle before calling offsetMap function
google.maps.event.addListener(map, 'idle', function () {
// Offset map
offsetMap();
});
// Listen for directions changes to update bounds and reapply offset
google.maps.event.addListener(directionsDisplay, 'directions_changed', function () {
// Get the updated route directions response
var updatedResponse = directionsDisplay.getDirections();
// Update route bounds
routeBounds = updatedResponse.routes[0].bounds;
// Fit updated bounds
map.fitBounds(routeBounds);
// Write directions steps
writeDirectionsSteps(updatedResponse.routes[0].legs[0].steps);
// Offset map
offsetMap();
});
}
});
}
function writeDirectionsSteps(steps) {
var overlayContent = document.getElementById("overlayContent");
overlayContent.innerHTML = '';
for (var i = 0; i < steps.length; i++) {
overlayContent.innerHTML += '<p>' + steps[i].instructions + '</p><small>' + steps[i].distance.text + '</small>';
}
}
function fitMap() {
if (routeBounds !== false) {
map.fitBounds(routeBounds);
}
}
function fitAndOffsetMap() {
if (routeBounds !== false) {
map.fitBounds(routeBounds);
offsetMap();
}
}
initialize();
body {
margin:0;
padding:0;
font-family: Arial;
}
#map-canvas {
height:450px;
width:950px;
}
#overlay {
position: absolute;
width: 200px;
height: 450px;
background: black;
opacity: .8;
top: 0;
left: 0;
overflow: auto;
}
#overlayContent {
color: white;
padding: 10px 20px;
}
#overlayContent p {
font-size: 12px;
margin: 6px 0;
}
#overlayContent small {
display: block;
text-align: right;
font-style: italic;
}
small {
font-size: 9px;
}
i {
color: lightblue;
}
h1 {
font-size: 20px;
}
h5 {
font-size: 12px;
}
button {
margin: 20px 0 0 20px;
}
<div id="map-canvas"></div>
<div id="overlay">
<div id="overlayContent">
<h1>DIV OVERLAY</h1>
<h5>Routes should not be drawn below this element.</h5>
<h5>Click the <i>Calc route</i> button to draw the directions route.</h5>
<h5><i>Map offset</i> will be applied automatically.</h5>
<h5><i>Drag the route</i> to see how it is applied.</h5>
<h5>Click the <i>Offset map</i> button to reapply the offset.</h5>
<h5>Click the <i>Fit only</i> button to only fit route bounds.</h5>
<h5>Click the <i>Fit and offset map</i> button to fit to route bounds and reapply offset.</h5>
</div>
</div>
<button id="calcRoute">Calc route</button>
<button id="offsetMap">Offset map</button>
<button id="fitMap">Fit only</button>
<button id="fitAndOffsetMap">Fit and offset map</button>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry" type="text/javascript"></script>

Mobile Safari Crashing with background-position css

I have the following code which crashes in mobile safari, works in all other browsers. I have a checkbox that is using jquery plugin for displaying a nicer image when it is checked. The issue seems to be in the background-position attribute support in Safari-mobile but I don't know how to fix it.
<input type="checkbox" id="terms" data-bind="checked: termsOfUseAccepted" >
<label for="terms" class="" data-bind="text:TermsLabel"></label>
<div class="chk-overview ">
<h2 >Continue</h2>
</div>
Following Javascript is used
$(function () {
var isTouch = false;
try { isTouch = "ontouchstart" in window; } catch (e) { }
var $activeTip = null;
if (isTouch) {
document.ontouchstart = function () {
if ($activeTip) {
$activeTip.data("close").call($activeTip);
$activeTip = null;
}
};
}
function courseViewModel() {
var self = this;
self.termsOfUseAccepted = ko.observable(false);
self.TermsLabel = ko.observable('I understand');
self.continueDisplay = ko.computed({
read: function() {
return self.termsOfUseAccepted();
},
owner: this,
deferEvaluation: true
});
};
var viewModel = new courseViewModel();
ko.applyBindings(viewModel);
});
(function($) {
$.fn.hoverIntent = function(f, g) {
var cfg = {sensitivity: 7,interval: 100,timeout: 0};
cfg = $.extend(cfg, g ? {over: f,out: g} : f);
var cX, cY, pX, pY;
var track = function(ev) {
cX = ev.pageX;
cY = ev.pageY
};
var compare = function(ev, ob) {
ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
if ((Math.abs(pX - cX) + Math.abs(pY - cY)) < cfg.sensitivity) {
$(ob).unbind("mousemove", track);
ob.hoverIntent_s = 1;
return cfg.over.apply(ob, [ev])
} else {
pX = cX;
pY = cY;
ob.hoverIntent_t = setTimeout(function() {
compare(ev, ob)
}, cfg.interval)
}
};
var delay = function(ev, ob) {
ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
ob.hoverIntent_s = 0;
return cfg.out.apply(ob, [ev])
};
var handleHover = function(e) {
var ev = jQuery.extend({}, e);
var ob = this;
if (ob.hoverIntent_t) {
ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t)
}
if (e.type == "mouseenter") {
pX = ev.pageX;
pY = ev.pageY;
$(ob).bind("mousemove", track);
if (ob.hoverIntent_s != 1) {
ob.hoverIntent_t = setTimeout(function() {
compare(ev, ob)
}, cfg.interval)
}
} else {
$(ob).unbind("mousemove", track);
if (ob.hoverIntent_s == 1) {
ob.hoverIntent_t = setTimeout(function() {
delay(ev, ob)
}, cfg.timeout)
}
}
};
return this.bind('mouseenter', handleHover).bind('mouseleave', handleHover)
}
})(jQuery);
(function($) {
jQuery.fn.customInput = function () {
$(this).each(function (i) {
if ($(this).is('[type=checkbox],[type=radio]')) {
var input = $(this);
if (input.data('customInput') === 'done') {
return true;
}
else {
input.data('customInput', 'done');
}
// get the associated label using the input's id
var label = $('label[for=' + input.attr('id') + ']');
//get type, for classname suffix
var inputType = (input.is('[type=checkbox]')) ? 'checkbox' : 'radio';
// wrap the input + label in a div
$('<div class="custom-' + inputType + '"></div>').insertBefore(input).append(input, label);
// find all inputs in this set using the shared name attribute
var allInputs = $('input[name=' + input.attr('name') + ']');
// necessary for browsers that don't support the :hover pseudo class on labels
label.hover(
function () {
$(this).addClass('hover');
if (inputType == 'checkbox' && input.is(':checked')) {
$(this).addClass('checkedHover');
}
},
function () { $(this).removeClass('hover checkedHover'); }
);
//bind custom event, trigger it, bind click,focus,blur events
input.bind('updateState', function () {
if (input.is(':checked')) {
if (input.is(':radio')) {
allInputs.each(function () {
$('label[for=' + $(this).attr('id') + ']').removeClass('checked');
});
};
label.addClass('checked');
}
else { label.removeClass('checked checkedHover checkedFocus'); }
})
.trigger('updateState')
.click(function () {
if (input.is(':checked')) {
if (input.is(':radio')) {
allInputs.each(function () {
$('label[for=' + $(this).attr('id') + ']').removeClass('checked');
});
};
label.addClass('checked');
}
else { label.removeClass('checked checkedHover checkedFocus'); }
})
.focus(function () {
label.addClass('focus');
if (inputType == 'checkbox' && input.is(':checked')) {
$(this).addClass('checkedFocus');
}
})
.blur(function () { label.removeClass('focus checkedFocus'); });
}
});
};
})(jQuery);
$.fn.smartHover = function (configObject) {
if (isTouch) {
$(this)
.bind("hold", function () {
$activeTip = $(this);
$(this).data("held", true);
})
.bind("hold", configObject.over)
.bind("click", function (e) {
var wasHeld = $(this).data("held");
$(this).data("held", false);
if (wasHeld) {
e.preventDefault();
return false;
}
})
.data("close", configObject.out);
} else {
$(this).hoverIntent(configObject);
}
};
$('input').customInput();
And here is the css
.chk-overview h2 { font: 24px "StoneSansITCW01-SemiBol 735693",sans-serif; margin-bottom: 20px;padding: 0 }
.custom-checkbox label {
background: transparent url(http://aonhewittnavigators.com/AonExchange/media/Image-Gallery/SiteImages/checkbox.png) no-repeat;
outline: 0;
background-position: 0 0;
}
.custom-checkbox label {
cursor: pointer;
display: block;
height: 19px;
outline: 0;
position: relative;
width: 21px;
z-index: 1;
}
.custom-checkbox label.checked {
background-position: 0 bottom;
padding: 0;
}
.custom-checkbox input {
left: 1px;
margin: 0;
outline: 0;
position: absolute;
top: 5px;
z-index: 0;
height: 0;
}
Try removing the height: 0 on the checkbox style. I have seen it crash when the height or width attribute is set on the checkbox input.

Resources