I just wrote this simple plugin for JQuery which lets you synchronize the scroll bars of any collection of elements. This lets you move the scrollbar of one div it have the scrollbars’ of the rest of the divs move the same exact amount.
Here is the code:
1: jQuery.fn.synchronizeScroll = function() {
2:
3: var elements = this;
4: if (elements.length <= 1) return;
5:
6: elements.scroll(
7: function() {
8: var left = $(this).scrollLeft();
9: var top = $(this).scrollTop();
10: elements.each(
11: function() {
12: if ($(this).scrollLeft() != left) $(this).scrollLeft(left);
13: if ($(this).scrollTop() != top) $(this).scrollTop(top);
14: }
15: );
16: });
17: }
Using this is SUPER simple. Lets say you have several divs defined as:
<div class=”scrollDiv” style=”overflow:auto;”> .. some large content</div>
To synchronize the scrollbars’ on all divs with the class scrollDiv all you need to write is:
$(“.scrollDiv”).synchronizeScroll();
I just wrote this simple plugin for JQuery which lets you synchronize the scroll bars of any collection of elements. This lets you move the scrollbar of one div it have the scrollbars’ of the rest of the divs move the same exact amount.
Here is the code:
1: jQuery.fn.synchronizeScroll = function() {
2:
3: var elements = this;
4: if (elements.length <= 1) return;
5:
6: elements.scroll(
7: function() {
8: var left = $(this).scrollLeft();
9: var top = $(this).scrollTop();
10: elements.each(
11: function() {
12: if ($(this).scrollLeft() != left) $(this).scrollLeft(left);
13: if ($(this).scrollTop() != top) $(this).scrollTop(top);
14: }
15: );
16: });
17: }
Using this is SUPER simple. Lets say you have several divs defined as:
<div class=”scrollDiv” style=”overflow:auto;”> .. some large content</div>
To synchronize the scrollbars’ on all divs with the class scrollDiv all you need to write is:
$(“.scrollDiv”).synchronizeScroll();
I just wrote this simple plugin for JQuery which lets you synchronize the scroll bars of any collection of elements. This lets you move the scrollbar of one div it have the scrollbars’ of the rest of the divs move the same exact amount.
Here is the code:
1: jQuery.fn.synchronizeScroll = function() {
2:
3: var elements = this;
4: if (elements.length <= 1) return;
5:
6: elements.scroll(
7: function() {
8: var left = $(this).scrollLeft();
9: var top = $(this).scrollTop();
10: elements.each(
11: function() {
12: if ($(this).scrollLeft() != left) $(this).scrollLeft(left);
13: if ($(this).scrollTop() != top) $(this).scrollTop(top);
14: }
15: );
16: });
17: }
Using this is SUPER simple. Lets say you have several divs defined as:
<div class=”scrollDiv” style=”overflow:auto;”> .. some large content</div>
To synchronize the scrollbars’ on all divs with the class scrollDiv all you need to write is:
$(“.scrollDiv”).synchronizeScroll();
I randomly yesterday started thinking about the unfoldr function in Haskell while working out at the gym (how nerdy is that, I am lifting iron but thinking of functional programming). Unfoldr take a single and an unfolding function and turns it into a list (the opposite of fold). At the gym I was thinking about an application where I can use this and I decided that when I got home I would use it to write a prime factorization function. This is a method that when given a number returns the list of its prime factors.
It was easy to write the only part I am not pleased about is the code I used to deal with tuples. It seems clumsy and I am still looking for a way to clean that up.
One note: The code below references a list of prime numbers called primes , which is not shown.
Here is the code:
1: primeFactors x = unfoldr findFactor x
2: where
3: first (a,b,c) = a
4: findFactor 1 = Nothing
5: findFactor b = (\(_,d,p)-> Just (p, d))
6: $ head $ filter ((==0).first)
7: $ map (\p -> (b `mod` p, b `div` p, p)) primes
This function will take any number which is greater than 1 and return a list of its prime factors. But don’t take my word for it, I wrote a quickcheck property to ensure the prime factors multiply back to the original number:
1: prop_factors num = num > 1 ==> num == (foldr1 (*) $ primeFactors num)
When running quickcheck on this property you see the following:
quickCheck prop_factors
OK, passed 100 tests.
I randomly yesterday started thinking about the unfoldr function in Haskell while working out at the gym (how nerdy is that, I am lifting iron but thinking of functional programming). Unfoldr take a single and an unfolding function and turns it into a list (the opposite of fold). At the gym I was thinking about an application where I can use this and I decided that when I got home I would use it to write a prime factorization function. This is a method that when given a number returns the list of its prime factors.
It was easy to write the only part I am not pleased about is the code I used to deal with tuples. It seems clumsy and I am still looking for a way to clean that up.
One note: The code below references a list of prime numbers called primes , which is not shown.
Here is the code:
1: primeFactors x = unfoldr findFactor x
2: where
3: first (a,b,c) = a
4: findFactor 1 = Nothing
5: findFactor b = (\(_,d,p)-> Just (p, d))
6: $ head $ filter ((==0).first)
7: $ map (\p -> (b `mod` p, b `div` p, p)) primes
This function will take any number which is greater than 1 and return a list of its prime factors. But don’t take my word for it, I wrote a quickcheck property to ensure the prime factors multiply back to the original number:
1: prop_factors num = num > 1 ==> num == (foldr1 (*) $ primeFactors num)
When running quickcheck on this property you see the following:
quickCheck prop_factors
OK, passed 100 tests.
I randomly yesterday started thinking about the unfoldr function in Haskell while working out at the gym (how nerdy is that, I am lifting iron but thinking of functional programming). Unfoldr take a single and an unfolding function and turns it into a list (the opposite of fold). At the gym I was thinking about an application where I can use this and I decided that when I got home I would use it to write a prime factorization function. This is a method that when given a number returns the list of its prime factors.
It was easy to write the only part I am not pleased about is the code I used to deal with tuples. It seems clumsy and I am still looking for a way to clean that up.
One note: The code below references a list of prime numbers called primes , which is not shown.
Here is the code:
1: primeFactors x = unfoldr findFactor x
2: where
3: first (a,b,c) = a
4: findFactor 1 = Nothing
5: findFactor b = (\(_,d,p)-> Just (p, d))
6: $ head $ filter ((==0).first)
7: $ map (\p -> (b `mod` p, b `div` p, p)) primes
This function will take any number which is greater than 1 and return a list of its prime factors. But don’t take my word for it, I wrote a quickcheck property to ensure the prime factors multiply back to the original number:
1: prop_factors num = num > 1 ==> num == (foldr1 (*) $ primeFactors num)
When running quickcheck on this property you see the following:
quickCheck prop_factors
OK, passed 100 tests.
HTML Source EditorWord wrap
I made 2 slight changes and I think it works pretty well now. I now detect if the browser is Firefox and use the regular resize event since its resize behaves like a resizeComplete. I also modified the timeout a bit since I think 100 ms might be too small.
Here is the updated version:
1: jQuery.fn.resizeComplete = function(callback)
2: {
3:
4: var element = this;
5: var height = element.height();
6: var width = element.width();
7: var monitoring = false;
8: var timer;
9:
10: function monitorResizing()
11: {
12: monitoring = true;
13:
14: var newHeight = element.height();
15: var newWidth = element.width();
16:
17: if(newHeight != height || newWidth != width)
18: {
19: height = newHeight;
20: width = newWidth;
21: timer = setTimeout(function() { monitorResizing() },200);
22: }
23: else
24: {
25: monitoring = false;
26: clearTimeout(timer);
27: callback();
28: }
29: }
30:
31: function onResize()
32: {
33: if(monitoring) return;
34: monitorResizing();
35: }
36:
37: if($.browser.mozilla)
38: {
39: element.resize(callback);
40: }
41: else
42: {
43: element.resize(onResize);
44: }
45:
46:
47: }
I have run into issues recently with the browsers’ implementation of the resize event on the window. Opera, Safari, IE and Firefox all have different behaviors when this event is fired.
-
- Firefox only fires it when you release the mouse.
- IE fires this event many many times while resizing.
- Safari will fire continusouly while dragging
- Opera fires after the resizing stopped
You can read more about this here.
These differences led me to search for a solution. I quickly wrote this proof of concept function today which may help solve this problem. It is a JQuery add-on called resizeComplete. It works by checking periodically after a resize starts and checks if the size of the object has changed. Once no change is detected it invokes the call back method.
This has not been tested much at all but I think it might be on the right track.
1: jQuery.fn.resizeComplete = function(callback)
2: {
3:
4: var element = this;
5: var height = element.height();
6: var width = element.width();
7: var monitoring = false;
8: var timer;
9:
10: function monitorResizing()
11: {
12: monitoring = true;
13:
14: var newHeight = element.height();
15: var newWidth = element.width();
16:
17: if(newHeight != height || newWidth != width)
18: {
19: height = newHeight;
20: width = newWidth;
21: timer = setTimeout(function() { monitorResizing() },100);
22: }
23: else
24: {
25: monitoring = false;
26: clearTimeout(timer);
27: callback();
28: }
29: }
30:
31: function onResize()
32: {
33: if(monitoring) return;
34: monitorResizing();
35: }
36:
37: element.resize(onResize);
38:
39: }
Here is an example of its usage:
1: $(document).ready(function()
2: {
3: $(window).resizeComplete(function(){ document.write('hi') });
4: }
5: );
The xUnit .Net codeplex page lists one useful Visual Studio code snippet for creating a Fact. As you can tell I am fairly fond of code snippets so I created a few more which I use when writing xUnit.net facts. These are one line snippets that I find very convenient when writing my assertions.
Below is a table which shows the shortcut you use to access the snippet and the code the snippet generates
| Shortcut |
Snippet |
| ae |
Assert.Equal($expected$,$actual$) |
| ane |
Assert.NotEqual($expected$,$actual$) |
| an |
Assert.Null($actual$) |
| ann |
Assert.NotNull($actual$) |
| at |
Assert.True($actual$) |
| af |
Assert.False($actual$) |
I have included a zip containing these snippets. Enjoy!
The xUnit .Net codeplex page lists one useful Visual Studio code snippet for creating a Fact. As you can tell I am fairly fond of code snippets so I created a few more which I use when writing xUnit.net facts. These are one line snippets that I find very convenient when writing my assertions.
Below is a table which shows the shortcut you use to access the snippet and the code the snippet generates
| Shortcut |
Snippet |
| ae |
Assert.Equal($expected$,$actual$) |
| ane |
Assert.NotEqual($expected$,$actual$) |
| an |
Assert.Null($actual$) |
| ann |
Assert.NotNull($actual$) |
| at |
Assert.True($actual$) |
| af |
Assert.False($actual$) |
I have included a zip containing these snippets. Enjoy!