This post is rated B for boringI wrote a little jquery shopping cart-ish thing for a project recently that I’m very happy with - viewable at http://alzbrant.ca/angel/ (It’s live, don’t submit test orders.) There’s three elements to this site, all of which I’ve been working on for quite some time:
I apologize if this is painfully banal to any readers with more than five minutes of knowledge with php, but this stuff is still very fresh and exciting to me; I’m still no real programmer, but just last year I didn’t know a line of php, and had never worked with any coding language since GWbasic. Maybe I’m not as dumb as I always believed, or maybe PHP is pretty dang easy. =D I don’t think I’m doing all that bad for a paint fumes-addled pencil junkie. I suppose in the end, both skillsets involve problem solving, and when it gets right down to it, what parts of life don’t? Things that are awesome:
|
December 7, 2007
September 14, 2007
Client throwing in far too many content? This will automatically abbreviate too-lengthy content with - read more - tags that activate on a mouseover. Then you can click to hide them again. It’ll try and break things at the nearest full stop, (”.”), or the nearest space if it can’t find a period. Hope someone finds this useful - you can see it in action over here.
/*
<ul class="accordion">
<li>Your content here.</li>
</ul>
*/
$(document).ready(function(){
//Snippet settings
var max_length = 110; // Maximum amount of characters to display
var margin_of_error = 80; // If your break point is closer than this value to the end of the string, it won't bother abbreviating it.
var read_more = "( Read More )"; // Message
var read_less = "( Click to hide )"; // Message
$(".accordion li").each(function(){
var length = $(this).html().length;
var text = $(this).html();
if (length >= max_length) {
var point = text.substr(max_length, length).indexOf(".");
if (point == 0) {
point = text.substr(max_length, length).indexOf(" ");
}
if (point + max_length + margin_of_error >= length) {
return; //too few characters to make a difference? Abbreviate.
}
var cutpoint = max_length + point + 1;
var newtext = text.substr(0, cutpoint);
var hiddentext = text.substr(cutpoint, length);
$(this).empty().append(newtext);
$(this).append("<span class=\"hidden\">" + hiddentext + "</span>");
$(this).append("<span class=\"read_more\">" + read_more + "</span>");
}
});
//On mouseover, expand.
$(".read_more").mouseover(function() {
if ($(this).prev(".hidden").css("display") == 'none')
{
if ( $(this).text() == read_more ) {
$(this).text(read_less);
}
else {
$(this).text(read_more);
}
$(this).prev(".hidden").slideDown(400);
}
});
//On mouseclick, hide.
$(".read_more").click(function() {
if ( $(this).text() == read_more ) {
$(this).text(read_less);
}
else {
$(this).text(read_more);
}
$(this).prev(".hidden").slideUp(400);
});
});
|
May 4, 2007
Jacob-Johnson.comI’m really happy with how the code turned out in this one. It’s a jquery-only portfolio site with auto-resizing popup images - imitating AJAX functionality without actually using it. The right sidebar is a vertical Jquery Carousel, with a few harassments to make it connect to the display window div. It’s basically an ordered list with a few validation-breaking attributes for jquery to pass as background-images to the left-side window. This allowed me to safely use massive images in a fixed-size space, as making them background-images esentially forces a clipping mask around the image. Ordered list structure <LI source="Carrackar.gif" ## full-size image description="Digital - Painter" ## description style="background-image:url(portfolio/Carrackar.gif);"> ##the thumbnail image <p>Carracker</p> ## title of the image </LI> Then I pass it to jquery with this snippet: $("#thumb_column li").click(function() {
$("#displaybox").css({"background-image": 'url(portfolio/' + $(this).attr('source') + ')' });
$("#expand_button a").attr("link","portfolio/" + $(this).attr('source'));
$("#expand_button p").empty().append( $(this).attr('description'));
$("#hidden").attr("src","portfolio/" + $(this).attr('source'));
});
Every time the user clicks on an image: But what’s that #hidden, you say? Why would you pass a link to something that’s not going to be seen? It’s because jQuery can use width() and height() on an img tag to get the image dimensions! This is the cool part. When the user clicks the ‘view as full window button’, it calls this function: $("#expand_button a").click(function() {
if ($(this).attr('link'))
{
var href = $(this).attr('link');
var description = $("#expand_button p").text();
var width = $("#hidden").width();
var height = $("#hidden").height();
new_window = window.open(href,'popup','width=' + width + ',height=' + height + 'toolbar=no,scrollbars=no,menubar=no');
new_window.document.write("<HTML><HEAD><TITLE>Jacob Johnson - Portfolio</TITLE>");
new_window.document.write("<script type=\"text/javascript\"> function resize(){ window.resizeTo(" + width + "," + height + "); }</script>");
new_window.document.write("<link href=\"popup.css\" rel=\"stylesheet\" type=\"text/css\"></HEAD><BODY onLoad=\"resize()\">");
new_window.document.write("<a href=\"javascript:window.close()\"><img src=\"" + href + "\" title=\"" + description +"\" id=\"popup_img\">");
new_window.document.write("</BODY></HTML>");
new_window.document.close();
}
});
As you see, it grabs the source attribute given to the button, and then gets dimensions from the #hidden image. It then creates a new popup window with those dimensions, and includes a supah-simple CSS file that removes the page margins. I hope this helps someone out there - I find jQuery to be a brilliant piece of code, and I’m really enjoying working with it. Lastly, visit Jake’s site, and throw money at him! |
All drupal-related paintings are licensed under a Creative Commons Attribution-Noncommercial 2.5 Canada License.
Everything else, all rights reserved, so hands off, you! :D


