Skip to content

Instantly share code, notes, and snippets.

@jeangontijo
jeangontijo / toggle_class_on_scroll.js
Last active May 27, 2019 00:25
Toggle Class on Scroll
window.addEventListener("scroll", function (event) {
var scroll = this.scrollY;
if(scroll > 0){
document.querySelector('nav').classList.add('scroll');
} else {
document.querySelector('nav').classList.remove('scroll');
}
});
@jeangontijo
jeangontijo / is_in_viewport.js
Created June 14, 2018 19:34
Change the class when the element is visible in the viewport
var visible = document.querySelectorAll('.element');
function isInViewport(el) {
var rect = el.getBoundingClientRect();
return (
// when any part of element is visible
rect.top >= -el.clientHeight &&
rect.left >= -el.clientWidth &&
rect.bottom <= window.innerHeight + el.clientHeight &&
rect.right <= window.innerWidth + el.clientWidth
@jeangontijo
jeangontijo / proximity_sensor.js
Created June 14, 2018 14:33
Proximity Sensor
document.addEventListener("mousemove", function (event) {
const x = event.pageX
const y = event.pageY
document.querySelectorAll("div").forEach(div => {
const dx = (div.offsetLeft + 50) - x
const dy = (div.offsetTop + 50) - y
const dist = Math.sqrt(dx * dx + dy * dy)
const score = Math.exp(dist * -0.003)
@jeangontijo
jeangontijo / simple_autoplay_slider.js
Created June 14, 2018 14:24
Simple Autoplay Slider with Mask
// HTML
// <div class="container">
// <img src="https://picsum.photos/1920/1079" alt="">
// <img src="https://picsum.photos/1920/1081" alt="">
// <img src="https://picsum.photos/1921/1081" alt="">
// <img src="https://picsum.photos/1922/1081" alt="">
// </div>
// SCSS
// .container {
@jeangontijo
jeangontijo / toggle_data_attribute_onclick.js
Created June 6, 2018 21:08
Toggle Data Attribute on Click
var nav = document.querySelector('.nav__toggle');
var toggleState = function (elem, one, two) {
var elem = document.querySelector(elem);
elem.setAttribute('data-state', elem.getAttribute('data-state') === one ? two : one);
};
nav.onclick = function (e) {
toggleState('.nav ul', 'closed', 'open');
e.preventDefault();
};
@jeangontijo
jeangontijo / floating_label.scss
Last active June 14, 2018 14:41
Floating Label
.floating-label {
position: relative;
margin-bottom: 10px;
label {
position: absolute;
top: calc(50% - 7px);
left: 0;
opacity: 0;
transition: all .3s ease;
}
@jeangontijo
jeangontijo / setviewport_onresize_function.js
Last active June 20, 2018 19:36
Set Viewport onResize Function
var ww;
var wh;
var setViewport = function () {
ww = window.innerWidth || document.documentElement.clientWidth;
wh = window.innerHeight || document.documentElement.clientHeight;
}
setViewport();
window.addEventListener('resize', function () {
setViewport();
@jeangontijo
jeangontijo / preloader.js
Last active June 14, 2018 14:39
Preloader
var interval = setInterval(function() {
if(document.readyState === 'complete') {
document.body.classList.remove('load');
clearInterval(interval);
}
}, 100);
@jeangontijo
jeangontijo / set_interval_change_class.js
Last active June 14, 2018 14:40
setInterval Change Class
var slider = document.getElementById("hero");
var classes = ['slide1', 'slide2', 'slide3', 'slide4'];
var myInterval = setInterval(function () {
changeClass()
}, 5000);
var i = 0;
function changeClass() {
@jeangontijo
jeangontijo / hover_on_touch.js
Last active June 14, 2018 14:40
Hover on Touch
// Replace :hover for .hover
var card = document.querySelectorAll('.card');
Array.prototype.slice.call(card).forEach(function(item) {
item.addEventListener('touchstart', function(e) {
this.classList.toggle("hover");
}, false);
item.addEventListener('mouseenter', function(e) {
this.classList.add("hover");
}, false);