Daily Archives

2 Articles

Scrolling in div without scrolling page

The goal: Having a div on a page that scrolls with the mousewheel or a swipe, but that doesn’t scroll the page itself when the user reaches the bottom or the top. There’s a number of solutions out there – but I found them to variously work only on desktop or only on mobile, or only on certain browsers. The following Javascript/JQuery snippet, used for Chicmi, works on modern browsers and on mobile.

$(document).ready(function() {

    var touchPosY;
    var scrollableDiv = $('#the-target-div');

    scrollableDiv.bind('touchstart', function(event) {
        touchPosY = event.originalEvent.touches[0].clientY;
    });

    scrollableDiv.bind('touchmove', function(event) {
        var touchDelta = touchPosY - event.originalEvent.changedTouches[0].clientY;
        if (touchDelta > 0 && scrollableDiv.scrollTop() + scrollableDiv.height() == scrollableDiv.prop('scrollHeight')) {
            event.preventDefault();
        } else if (touchDelta < 0 && scrollableDiv.scrollTop() == 0) {
            event.preventDefault();
        }
    });

    scrollableDiv.bind('wheel', function(event) {
        if (event.originalEvent.deltaY > 0 && scrollableDiv.scrollTop() + scrollableDiv.height() == scrollableDiv.prop('scrollHeight')) {
            event.preventDefault();
        } else if (event.originalEvent.deltaY < 0 && scrollableDiv.scrollTop() == 0) {
            event.preventDefault();
        }
    });

});

Replicating PHP’s Adler32 hash in Python

If you’ve got to replicate a PHP hash('adler32', $string) command in Python, you may expect to just do this:

from zlib import adler32
checksum = adler32(string)

Unfortunately the values are different, for two reasons. Firstly, Python returns the result in decimal, while PHP returns the result in hex. Secondly, Python returns a signed value on some platforms and an unsigned value on others, so even within the same Python code you might not always get the same result.

Therefore, to get the same value on all platforms, and match the value you get back from PHP, you’ll need to use this:

from zlib import adler32
str(hex(adler32(email['email']) & 0xffffffff))[2:]