Skip to content

Instantly share code, notes, and snippets.

@rossmc
Last active August 29, 2015 14:05
Show Gist options
  • Save rossmc/caac945899b46bd72475 to your computer and use it in GitHub Desktop.
Save rossmc/caac945899b46bd72475 to your computer and use it in GitHub Desktop.
useful javascript
$(document).ready(function(){
// http://underscorejs.org/ for lots of useful functions
var jQuery = jQuery.noConflict(); // resolve jQuery conflict
// back previous URL on blog post page
$('#browse-back-btn').click(function(){
window.history.back();
});
var scrollY = window.pageYOffset; // get y-axis scroll position
var scrollX = window.pageXOffset; // get y-axis scroll position
/***********************************
Get Viewport Width & Height
***********************************/
var vw = $( window ).width();
var vh = $( window ).height();
// get viewport width & height on browser resize
$( window ).resize(function() {
vw = $( window ).width();
});
/***********************************
Checks for Apple Device
***********************************/
var ios = ( navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false );
var isiPhone = ( navigator.userAgent.match(/(iPhone|iPod)/g) ? true : false);
var iphone4 = (window.screen.height == (960 / 2));
var iphone5 = (window.screen.height == (1136 / 2));
/***********************************
Checks for Browser
***********************************/
var is_chrome = navigator.userAgent.indexOf('Chrome') > -1;
// IE 11 has different user agent string than all other IE
var is_explorer = (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0);
var is_firefox = navigator.userAgent.indexOf('Firefox') > -1;
var is_Opera = navigator.userAgent.indexOf("Presto") > -1;
// Chrome has both 'Chrome' and 'Safari' inside userAgent string
var isSafari = (navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Chrome') == -1);
/***********************************
Check if user is using touch device
***********************************/
function isTouchDevice() {
return 'ontouchstart' in window // works on most browsers
|| 'onmsgesturechange' in window; // works on ie10
};
if(isTouchDevice()===true) {
console.log('Touch Device'); //your logic for touch device
}
else {
console.log('Not a Touch Device'); //your logic for non touch device
}
/***********************************
Event Listeners
***********************************/
// addEventListener() method to attach a scroll event on the window , calling a onScroll function
window.addEventListener("scroll", onScroll, false);
$(window).scroll(function(){
console.log('Window is scrolling'); //your logic here
});
/***********************************
Performs a smooth page scroll to an anchor on the same page.
***********************************/
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
/***********************************
Toggle between click events
***********************************/
$( "#selector" ).click(function(){
$(this).toggleClass('clicked');
if(!($(this).hasClass("clicked"))){
// Your logic here
}else{
// Your logic here
}
});
/***********************************
floating sidebar stops at footer
***********************************/
function floatingSideNavFooterOffSet(sidebar, footer) {
var scrollVal = $(document).height() - $(this).scrollTop();
var footerHeight = $(".footer-container").height();
var docHeight = $(document).height();
var floatingSideNav = $(".add-to-cart-wrapper .floating-wrapper").height();
var floatingSideNavFooterOffSetVal = docHeight - footerHeight -floatingSideNav - 190;
if ( scrollVal < 940 ) {
$('.add-to-cart-wrapper .floating-wrapper').css({'position':'absolute', 'top': floatingSideNavFooterOffSetVal});
} else {
$('.add-to-cart-wrapper .floating-wrapper').css({'position':'fixed', 'top': '216px'});
}
}
$(window).scroll(function() {
floatingSideNavFooterOffSet('.siderbar', '#footer');
});
$('.btn').click(function() {
floatingSideNavFooterOffSet('.siderbar', '#footer');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment