Coder Social home page Coder Social logo

useful-snippets's Introduction

useful-snippets

Useful snippets of CSS, Sass and JavaScript from and for my projects.

Feel free to use.

Table of contents

  1. Sass
  2. JavaScript
  3. Useful Links

Sass

  1. Mixins

Small snippets of code for faster use on projects. Feel free to use or contribute if you think some of snippets can be better written!

Mixins

Snippets for Sass containing some basic variable names which needs to be created before using mixins.

Font size from px to em

Mixin changing px font size to em font size.

$base-font-size: 16px;
@mixin font-size($font-size) {
    font-size: 1em * $font-size/$base-font-size;
}

example of usage

.foo {
    @include font-size(16px);
}

Retina background images

Mixin changes background images from @1x to @2x on retina screens

@mixin bg-retina($file, $type) {
    background-image: url($file + '.' + $type);
    
    @media (-webkit-min-device-pixel-ratio: 2), (-moz-min-device-pixel-ratio: 2) {
        & {
            background-image: url($file + '@2x.' + $type);
        }
    }
}

example of usage

.foo {
    @include bg-retina('path/to/file/example','svg');
}

Placeholder attributes

Mixin changes attributes of placeholder with all neccessary prefixes

@mixin placeholder {

    &.placeholder { 
        @content; 
    }
    
    &:-moz-placeholder { 
        @content; 
    }
    
    &::-moz-placeholder { 
        @content; 
    }
    
    &:-ms-input-placeholder { 
        @content; 
    }
    
    &::-webkit-input-placeholder { 
        @content; 
    }
}

example of usage

.foo {
    @include placeholder{
        color: $red;
    }
}

Center block

Mixin center block within parent

@mixin center-block {
    margin: { 
        left: auto;
        right: auto;
    }
}   

example of usage

.foo {
    @include center-block
}

Transition

Mixin helps with transition value

@mixin transition($args...) {
    -webkit-transition: $args;
    -moz-transition: $args;
    -ms-transition: $args;
    -o-transition: $args;
    transition: $args;
}

example of usage

.foo {
    @include transition(0.5s all);
}

#JavaScript

  1. Functions

##Functions

Retina images

Function changes images from @1x to @2x on retina screens.

if (window.devicePixelRatio > 1) {
    var lowresImages = $('img');
    lowresImages.each(function(i) {
        var lowres = $(this).attr('src');
        var highres = lowres.replace(".", "@2x.");
        $(this).attr('src', highres);
    });
}

Is in viewport

Function check if element is in viewport and returns true or false

$.fn.isOnScreen = function () {

    var win = $(window);

    var viewport = {
      top: win.scrollTop(),
      left: win.scrollLeft()
    };
    viewport.right = viewport.left + win.width();
    viewport.bottom = viewport.top + win.height();

    var bounds = this.offset();
    bounds.right = bounds.left + this.outerWidth();
    bounds.bottom = bounds.top + this.outerHeight();

    return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));

  };

example of usage

$('.element').isOnScreen()

Useful Links

Oh My ZSH - Terminal Plugin

Plugin for terminal - fav theme: YS

http://ohmyz.sh/

https://github.com/robbyrussell/oh-my-zsh/blob/master/themes/ys.zsh-theme

Online Image Color Extractor

Makes CSS gradient from image

http://www.louisbourque.ca/Color-Extractor/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.