Skip to content

Instantly share code, notes, and snippets.

@Farmatique
Farmatique / gist:0e131b434141fe7d4d9e9c871393000d
Created May 4, 2020 14:59
Check if element in viewport (even NOT FULL element, but partially)
function isScrolledIntoView(elem){
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elemTop = $(elem).offset().top;
var elemBottom = elemTop + $(elem).height();
return Math.min(elemBottom, docViewBottom) >= Math.max(elemTop, docViewTop);
}
@Farmatique
Farmatique / gist:536f04b3ac905189a74228c262036435
Created April 30, 2020 14:24
Fixed button scroll to top appear on scrolling from top
document.addEventListener('DOMContentLoaded', function() {
function throttle(fn, threshhold, scope) {
threshhold || (threshhold = 250);
var last,
deferTimer;
return function () {
var context = scope || this;
var now = +new Date,
args = arguments;
@Farmatique
Farmatique / gist:303085fd61853ae55befe4368db284c7
Created April 28, 2020 10:06
Wordpress redirect all 404 to homepage
/* put code below in 404.php */
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".get_bloginfo('url'));
exit();
?>
@Farmatique
Farmatique / gist:daac5b1a6b4998bca3c9eec1b90c5ccd
Created March 13, 2020 13:04
Align icon (image element, etc) with only first line of text
<h1>The long long text with icon before that should be vertically aligned to first line of text<h1>
h1{
font-size: 40px;
line-height: 1.444;
}
h1:before{
content: 'A'; // here is the first trick: pseudoelement with some text gives us the height of the 1st line of main-element`s text
display: inline-block;
background: url(youricon.png) no-repeat;
@Farmatique
Farmatique / gist:fe6bcd495f9f79e330d6bcda7d0c5464
Created February 27, 2020 11:27
Vue.js issue with handling event on v-html rendering
When trying to catch event that fires on rendering the v-html you should be aware of that its actually renders html ONLY AFTER ANIMATION/TRANSTION is finished. So, thats why you need to create setTimeout. Just try to omit <transition> and see that it catch rendering correctly
@Farmatique
Farmatique / gist:938f5168469e0685e2c4eba05412fbb7
Created February 12, 2020 11:59
Get current url inside wordpress (php)
function current_url()
{
$url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$validURL = str_replace("&", "&amp", $url);
return $validURL;
}
@Farmatique
Farmatique / gist:308e16b4d85e214b6af86b02f94d8338
Last active February 4, 2020 13:59
Wordpress: Search in ACF fields loop
<form method="GET" action="/url-of-your-current-page/">
<input type="text" name="search" class="search-string" placeholder="">
<a class="search-clear"><i class="fa fa-times-circle" aria-hidden="true"></i>
</a>
<button type="submit">Search</button>
</form>
//search block js
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
@Farmatique
Farmatique / gist:26d28f1e9685453da6f37c35c7daa4ed
Created December 24, 2019 12:41
Add search criteria parameter to WP Query
$search_str = get_query_var('search') ? get_query_var('search') : ''; // get string from url ?search=search_string (e.g. that what is submitted by ordinary form)
$args = array(
's' => $search_str //add parameter to query args
);
$the_query = new WP_Query($args); // standart WP query call
@Farmatique
Farmatique / gist:7105f25893fba5fb559df8190343960a
Created December 6, 2019 10:39
Remove search query from URL including '?'
window.location.href = window.location.href.split('?')[0]
@Farmatique
Farmatique / gist:952f07a1bb53e7000258f80092a7b37f
Last active December 4, 2019 11:47
Filtering array leaving only unique values
uniqueValuesArray = inputArray.filter(function(item, pos) {
return inputArray.indexOf(item) == pos;
})
//slightly different
uniqueValuesArray = inputArray.filter(function(item, pos, self) {
return self.indexOf(item) == pos;
})