Reading List
The most recent articles from a list of feeds I subscribe to.
Cross-browser imageless linear gradients v2
A while ago, I posted a script of mine for creating 2-color cross-browser imageless linear gradients. As I stated there, I needed them for a color picker I have to create. And even though 2-color gradients are sufficient for most components, in most color spaces, I had forgotten an important one: Hue. You can’t represent Hue with a 2-color gradient! So, I had to revise the script, and make it able to produce linear gradients of more than 2 colors. Furthermore, I needed to be able to specify a fully transparent color as one of the gradient colors, in order to create the photoshop-like 2d plane used by the picker (and no, a static image background like the one used in most JS color pickers wouldn’t suffice, for reasons irrelevant with this post). I hereby present you Cross-browser, imageless, linear gradients v2!
The API has stayed just the same, with the following differences:
-
You may specify the keyword “transparent” instead of a #RRGGBB color (that was such a pain to implement btw!).
-
When creating a Gradient object, color strings are now defined in an array. Example:
var g = new Gradient(200, 100, [‘#000000’, ‘#ff1166’, ‘#23ff46’], true);
-
When calling
g.paint()it now takes 2 arguments instead of 3: The new color array (or null if you don’t want that to change) and the direction (true for vertical, false for horizontal). For example:g.paint([‘#000000’, ‘#ff1166’, ‘#23ff46’], true);
-
2 new methods have been added:
g.setColorAt(index, color)andg.direction(newDirection). The first allows you to set a particular gradient color (index starting from 0) and the second to alter or toggle the direction (if you specify a direction parameter, you set the direction, if you call it with no parameters, it toggles from horizontal to vertical). -
The fields
g.startColorandg.endColorhave been replaced by the arrayg.colors.
Update: v2.0.1 Fixed a small bug with the ‘transparent’ keyword that affected multi-color gradients in browsers != IE when the transparent color wasn’t first or last.
Enjoy:
Cross-browser imageless linear gradients v2
A while ago, I posted a script of mine for creating 2-color cross-browser imageless linear gradients. As I stated there, I needed them for a color picker I have to create. And even though 2-color gradients are sufficient for most components, in most color spaces, I had forgotten an important one: Hue. You can’t represent Hue with a 2-color gradient! So, I had to revise the script, and make it able to produce linear gradients of more than 2 colors. Furthermore, I needed to be able to specify a fully transparent color as one of the gradient colors, in order to create the photoshop-like 2d plane used by the picker (and no, a static image background like the one used in most JS color pickers wouldn’t suffice, for reasons irrelevant with this post). I hereby present you Cross-browser, imageless, linear gradients v2!
The API has stayed just the same, with the following differences:
-
You may specify the keyword “transparent” instead of a #RRGGBB color (that was such a pain to implement btw!).
-
When creating a Gradient object, color strings are now defined in an array. Example:
var g = new Gradient(200, 100, [‘#000000’, ‘#ff1166’, ‘#23ff46’], true);
-
When calling
g.paint()it now takes 2 arguments instead of 3: The new color array (or null if you don’t want that to change) and the direction (true for vertical, false for horizontal). For example:g.paint([‘#000000’, ‘#ff1166’, ‘#23ff46’], true);
-
2 new methods have been added:
g.setColorAt(index, color)andg.direction(newDirection). The first allows you to set a particular gradient color (index starting from 0) and the second to alter or toggle the direction (if you specify a direction parameter, you set the direction, if you call it with no parameters, it toggles from horizontal to vertical). -
The fields
g.startColorandg.endColorhave been replaced by the arrayg.colors.
Update: v2.0.1 Fixed a small bug with the ‘transparent’ keyword that affected multi-color gradients in browsers != IE when the transparent color wasn’t first or last.
Enjoy:
Java pretty dates
First of all, sorry for not posting as frequently as before. I’m feverishly working on a new project with a really tight deadline and I don’t have as much time as I previously did.
For reasons that are irrelevant to this post, I have to write lots of Java code. So, sorry if I disappoint my fellow readers, but this post isn’t about JavaScript or CSS, it’s about Java. I wanted to display “pretty dates” (a bit like Twitter’s, for example “yesterday”, “5 minutes ago”, “last year” and so on) in a few places and I couldn’t find a Java implementation, so I decided to code my own.
For anyone that might need it, here it is:
import java.util.Date;
/**
* Class for human-readable, pretty date formatting
* @author Lea Verou
*/
public class PrettyDate
{
private Date date;
public PrettyDate() {
this(new Date());
}
public PrettyDate(Date date) {
this.date = date;
}
public String toString() {
long current = (new Date()).getTime(),
timestamp = date.getTime(),
diff = (current - timestamp)/1000;
int amount = 0;
String what = "";
/**
* Second counts
* 3600: hour
* 86400: day
* 604800: week
* 2592000: month
* 31536000: year
*/
if(diff > 31536000) {
amount = (int)(diff/31536000);
what = "year";
}
else if(diff > 2592000) {
amount = (int)(diff/2592000);
what = "month";
}
else if(diff > 604800) {
amount = (int)(diff/604800);
what = "week";
}
else if(diff > 86400) {
amount = (int)(diff/86400);
what = "day";
}
else if(diff > 3600) {
amount = (int)(diff/3600);
what = "hour";
}
else if(diff > 60) {
amount = (int)(diff/60);
what = "minute";
}
else {
amount = (int)diff;
what = "second";
if(amount < 6) {
return "Just now";
}
}
if(amount == 1) {
if(what.equals("day")) {
return "Yesterday";
}
else if(what.equals("week") || what.equals("month") || what.equals("year")) {
return "Last " + what;
}
}
else {
what += "s";
}
return amount + " " + what + " ago";
}
}
Hope someone finds it useful. :)
Java pretty dates
First of all, sorry for not posting as frequently as before. I’m feverishly working on a new project with a really tight deadline and I don’t have as much time as I previously did.
For reasons that are irrelevant to this post, I have to write lots of Java code. So, sorry if I disappoint my fellow readers, but this post isn’t about JavaScript or CSS, it’s about Java. I wanted to display “pretty dates” (a bit like Twitter’s, for example “yesterday”, “5 minutes ago”, “last year” and so on) in a few places and I couldn’t find a Java implementation, so I decided to code my own.
For anyone that might need it, here it is:
import java.util.Date;
/**
* Class for human-readable, pretty date formatting
* @author Lea Verou
*/
public class PrettyDate
{
private Date date;
public PrettyDate() {
this(new Date());
}
public PrettyDate(Date date) {
this.date = date;
}
public String toString() {
long current = (new Date()).getTime(),
timestamp = date.getTime(),
diff = (current - timestamp)/1000;
int amount = 0;
String what = "";
/**
* Second counts
* 3600: hour
* 86400: day
* 604800: week
* 2592000: month
* 31536000: year
*/
if(diff > 31536000) {
amount = (int)(diff/31536000);
what = "year";
}
else if(diff > 2592000) {
amount = (int)(diff/2592000);
what = "month";
}
else if(diff > 604800) {
amount = (int)(diff/604800);
what = "week";
}
else if(diff > 86400) {
amount = (int)(diff/86400);
what = "day";
}
else if(diff > 3600) {
amount = (int)(diff/3600);
what = "hour";
}
else if(diff > 60) {
amount = (int)(diff/60);
what = "minute";
}
else {
amount = (int)diff;
what = "second";
if(amount < 6) {
return "Just now";
}
}
if(amount == 1) {
if(what.equals("day")) {
return "Yesterday";
}
else if(what.equals("week") || what.equals("month") || what.equals("year")) {
return "Last " + what;
}
}
else {
what += "s";
}
return amount + " " + what + " ago";
}
}
Hope someone finds it useful. :)
Better usability in 5 minutes
In this post I’m going to share some tips to increase a site’s usability that are very quick to implement. Not all of them are cross-browser, but they are the icing on the cake anyway, nobody would mind without them.
1. Make buttons and button-like links appear pressed
This is a personal favorite. When you use CSS to style a button, or when you use an image (either as a background image or in the tag) to depict a fancy button, it will remain the same when being pressed in some or all browsers (depending on the case). You can use this easy trick to let the user know that he actually clicked something that is, indeed, clickable:
.mybutton:active { position:relative; top: 1px; left: 1px; }
which actually moves the button 1 pixel to the right and 1 pixel to the bottom when it’s being clicked. Try it, it’s actually quite convincing.
Other, equally quick options are: making the border inset, giving to the text a text-indent of 1px, reversing a gradient background (if you already use the reversed version somewhere else in the site, it is quick since you don’t have to use an image editor just for that), or a combination of them.
2. Smooth transitions
This is a webkit-only tip, but as I said, it’s just the icing on the cake, so who cares? If a smooth transition is crucial to your design, by all means, write a script for that or use a library. If you were planning to go the CSS-only way anyway, this will significantly increase the user experience for webkit users.
Let’s suppose that the links in your page are normally blue, and red on hover. To make the transition from blue to red smooth for webkit users, only 2 lines are needed in the CSS:
a { color:blue; transition-property: color; transition-duration: 1s; }
a:hover { color:red; }
The first one (transition-property) tells the browser which CSS property to smoothly transition and the second one (transition-duration) how long you want the whole effect to last. It’s important to place those in the normal CSS rule and not the one with the :hover pseudoclass, because otherwise there will be no transition when the user mouses out of the element. Please note that you currently need to also include browser prefixes for these properties or just use -prefix-free.
3. Add dingbats to buttons that depict their functionality
We all know that most browsers don’t like dingbat-only fonts. However, there are some dingbats that are available in most web-safe unicode fonts. For instance, review the following examples:
Without dingbats:
Next Previous Done Favorite
With dingbats:
Next → ← Previous ✔ Done ♥ Favorite
There are named html entities for some of them, others have to be used by their hex unicode index like ꯍ (you have to test the last ones a lot, since not all are web-safe enough).
You can find many such dingbats with their unicode hex codes in http://www.copypastecharacter.com/ and http://www.alanwood.net/unicode/dingbats.html.
Of course, if you have the time, by all means, use normal icons. If you don’t however, I find symbols to be a handy alternative. Sometimes I also use them as icon placeholders in work in progress until I find the time to design real icons.
4. Zebra rows
This won’t work on IE and Firefox 3. You can increase readability of tables and some types of lists by slightly alternating the background color of the rows. You’ve probably seen this effect numerous times and it’s usually done via JavaScript or the server side code that generates the table. You can quickly do it with plain CSS3 however, if you don’t mind it not working in IE and older browser versions or don’t have the time for a complete cross-browser solution:
table.stats tr { background:white; }
table.stats tr:nth-child(odd) { background:#f4f4f4; }
5. Highlight the current target
This won’t work in IE and older browser versions. If a particular page has lots of content, navigable by anchors (for example a FAQ page), you can use the CSS3 :target pseudo-class to let the user know where they landed:
h3:target { background:#FFFBCC; }
The h3 will only get a #FFFBCC background when it’s actually the landing point for the user. For example, if it has the id “foo”, it will get an #FFFBCC background when the user navigates to #foo.
That’s all folks
Did it actually take more than 5 minutes? ;)