Rational Z-Index Values

A reader named Arjan recently emailed me telling me about a website that The Printliminator didn’t work on. They had traced it back to the fact that the site used a bunch of “overlays” with ridiculously high values for z-index. …

Avatar of Chris Coyier
Chris Coyier on

Background Desires

The background property is a major player in what makes the awesome CSS designs of today possible. There are just a few properties that make it up: background-color, background-image, background-position, background-repeat, and background-attachment. Very simple, very powerful. I have a …

Avatar of Chris Coyier
Chris Coyier on

Curating Comments Threads

Long comment threads on blog posts are a mixed blessing. It is great to have stirred up such great community discussion. But anything beyond, say, 20 comments is beginning to get beyond what anyone is willing to actually read. What …

Avatar of Chris Coyier
Chris Coyier on (Updated on )

Truncate String by Words

Technique #1
<?php
function trunc($phrase, $max_words) {
   $phrase_array = explode(' ',$phrase);
   if(count($phrase_array) > $max_words && $max_words > 0)
      $phrase = implode(' ',array_slice($phrase_array, 0, $max_words)).'...';
   return $phrase;
}
?>
Technique #2
function limit_words($words, $limit, $append = ' &hellip;') {
       // Add 
Avatar of Chris Coyier
Chris Coyier on (Updated on )

Call Function with Random Timer

function randRange(data) {
       var newTime = data[Math.floor(data.length * Math.random())];
       return newTime;
}

function toggleSomething() {
       var timeArray = new Array(200, 300, 150, 250, 2000, 3000, 1000, 1500);

       // do stuff, happens to use jQuery here (nothing else does)
       $("#box").toggleClass("visible");

       clearInterval(timer);
       
Avatar of Chris Coyier
Chris Coyier on

Random Ad Positions

If you have multiple display ads of the same size in a block on your site, a good thing to do is randomize their position in the block. It’s more fair to the advertisers and the different layouts reduce ad …

Avatar of Chris Coyier
Chris Coyier on

Scroll Page Horizontally With Mouse Wheel

1) Load jQuery and the Mouse Wheel plugin

Mouse Wheel plugin is here.

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js?ver=1.3.2'></script>
<script type='text/javascript' src='/js/jquery.mousewheel.min.js'></script>

2) Attach mousewheel event to body

The “30” represents speed. preventDefault ensures the page won’t scroll down.

$(function() {

   $("body").mousewheel(function(event, 
Avatar of Chris Coyier
Chris Coyier on (Updated on )

Shuffle DOM Elements

This is from James Padolsey. Check out his article for a pure JavaScript technique as well.

Plugin
(function($){
 
    $.fn.shuffle = function() {
 
        var allElems = this.get(),
            getRandom = function(max) {
                return Math.floor(Math.random() * max);
            },
            shuffled = $.map(allElems, function(){
                var 
Avatar of Chris Coyier
Chris Coyier on

Filtering Blocks v2

This is an update to the first version of filtering blocks I did a while back. The idea is that you have a long list or large set of “blocks” on the page. Each block belongs to a certain group. …

Avatar of Chris Coyier
Chris Coyier on