Skip to content

Instantly share code, notes, and snippets.

@louisnovick
louisnovick / distance.js
Created September 15, 2017 13:15
Calculate distance between elements
const getPositionAtCenter = function (element) {
let data = element.getBoundingClientRect();
return {
x: data.left + data.width / 2,
y: data.top + data.height / 2
};
};
const getDistanceBetweenElements = function(a, b) {
let aPosition = getPositionAtCenter(a);
@louisnovick
louisnovick / htfind.php
Created May 17, 2016 17:25
What is the path to your .htpasswd file?
<?php
$dir = dirname(__FILE__);
echo "<p>Full path to this dir: " . $dir . "</p>";
echo "<p>Full path to a .htpasswd file in this dir: " . $dir . "/.htpasswd" . "</p>";
?>
function wpse_128636_redirect_post() {
$queried_post_type = get_query_var('post_type');
if ( is_single() && 'post_type' == $queried_post_type ) {
wp_redirect( home_url(), 301 );
exit;
}
}
@louisnovick
louisnovick / shrinkmenu.js
Last active May 3, 2016 16:46
Shrinking menu on scroll
(function($){
$(function(){
var lastScrollTop = 0, delta = 5;
$(window).scroll(function(event){
var st = $(this).scrollTop();
if( $(window).scrollTop() > 200 ) {
if(Math.abs(lastScrollTop - st) <= delta)
return;
if (st > lastScrollTop){
@louisnovick
louisnovick / lazyYT.php
Created July 2, 2015 13:50
Create a Wordpress shortcode for lazy loading videos
function lazy_load_videos($atts) {
$a = shortcode_atts( array(
'id' => 'something else',
'width' => '',
'height' => ''
), $atts );
$stringbean = '<div class="js-lazyYT ';
if( !empty($a['height']) ){
$stringbean .= '" data-height="'.$a['height'].'" style="height:'.$a['height'].'; width:'.$a['width'].';" data-width="'.$a['width'].'"';
}else{
@louisnovick
louisnovick / scroll.js
Created July 2, 2015 13:48
Smooth Scroll. Create a back to top button that appears based on window height.
$(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;
@louisnovick
louisnovick / placeholder.js
Created July 2, 2015 13:44
Replaces input field placeholders with text until focused
$('[placeholder]').focus(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
}).blur(function() {
var input = $(this);
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('placeholder');
@louisnovick
louisnovick / foundation-flexvid.js
Created May 20, 2015 18:36
Used Foundation to make an old Wordpress site responsive. The old site had a lot of old video embeds wrapped in <object>. This will wrap all object tags with Foundation's flex video css making them responsive.
$( "object" ).wrap( "<div class='flex-video widescreen vimeo'></div>" );