Category Archives: JQuery

At Cheezburger, my team is currently working on a sharing widget for our recently released list post type. If you take a look at this list, you will see our MVP of the idea. You will notice a share box in the left gutter that appears to float in a fixed position while you scroll through the items in the list giving you the opportunity to share at any point.

We are using an awesome little jQuery plugin called Waypoints to accomplish this. Waypoints makes it easy to execute a function whenever you scroll to an element. Our implementation looks like this.

In this code we are attaching our waypoint to the footer of of the page. When the footer scrolls into the window viewport the affix method gets called. The affix method simply toggles the is fixed class on the share widget. We are also passing in an options object that tells the waypoint to trigger when the footer scrolls passed the point 75% from the top of the viewport.

In our next iteration, we are planning to change the way the share button behaves as you scroll through the list. The idea being when you click the button while it is within the area of the third list item, the image and title used to share the list on Facebook will be the third list items information. So while you scroll though the list we are updating the share buttons shared information.

We have not implemented this functionality yet, but yesterday I did a spike to figure out how we could accomplish it. And once again, Waypoints made the task easy.

In this code I am attaching a waypoint to each list asset item. But this time I am setting the offset to the fixed pixel position of the top of the share widget. This allows me to trigger the onSwitch function as the top of the list asset item crosses the top of the share widget. The handler is scoped correctly for the list asset item so I can easily grab it’s index, title and description. Waypoints will even tell me which direction the user is scrolling.

I recently started playing around with the new jQuery template plugin after seeing Stephen Walther present at my local .NET Users Group. I wanted to share this simple user notification template that I have been using.

It is amazing how little code is needed create a very nice and consistent ajax error notification.

Update: I wanted to pop back into this post seeing as how it is getting some circulation on the internets and go into a little detail on what all is going on here. To start this code sample is a Razor based layout in an ASP.NET MVC 3 application. So, it is the equivalent of a Master Page in ASP.NET. The elements that being with ‘@’ are Razor syntax for rendering server side information into the final markup sent to the browser.

The page consists of a couple divs one of which is classed “ajax-busy” that contains a typical ajax spinner image. While not in the sample code, the ajax-buzy class is not visible by default using CSS rules. The main script block uses the jQuery ajax methods to display the spinner when there is an active ajax call being made.

Near the bottom of the page is a script block of type text/html, this is my client side template for my error notification. This block is ignored by browsers while rendering. To display a message, I add a handler to jQuery’s ajaxError method. This handler first locates the template element by id, then applies the template to a json object that I have created inline and finally appends the result to the body of the html document.

The element is now visible, but I take it a step farther by displaying it as a model dialog using jQuery UI’s dialog widget. With some simple set up of the dialog I handle removing the element from the page when the OK button is clicked.

This bit of functionality is now handled globally in the application by this one chunk of code. Here is what the end product looks like.