Skip to content

Instantly share code, notes, and snippets.

@pixelhijack
pixelhijack / js: cross browser mouse event coordinates
Last active September 30, 2016 16:55
get exact cross browser mouse event coordinates workarounds
/*
get exact cross browser mouse event coordinates workarounds (relative to offset parent)
*/
/* with jquery */
var x = event.offsetX || event.clientX - $(event.target).offset().left,
y = event.offsetY || event.clientY - $(event.target).offset().top;
/* on svg raphael paper */
/*
HELPER - extend raphael: draggable elements
*/
Raphael.el.draggable = function(){
//state
this.attr({cursor: "move"});
this.drag(
//move
function(dx,dy){
/*
Raphael.js: unbind all event listeners from an element
*/
Raphael.el.unbindAll = function(){
while(this.events.length){
var e = this.events.pop();
e.unbind();
};
};
/*
extending Raphael.js: draggable sets
*/
Raphael.st.draggable = function() {
var _this = this,
lx = 0,
ly = 0,
ox = 0,
oy = 0,
/*
DRAGGABLE PATHS IN RAPHAEL
hint: convert the x and y deltas into translate values which the path object understands
*/
Raphael.el.draggablePath = function(){
this.attr({cursor: "move"});
this.drag(
function(dx,dy){
this.translate(dx - this.odx, dy - this.ody);
this.odx = dx;
/*
export whole canvas as JSON via RaphaelJSON
*/
exportJSON:function(){
var _this = this,
json = this.paper.toJSON(function(el, data){
data.ft = {};
if ( el.freeTransform != null ){
data.ft.attrs = el.freeTransform.attrs;
/*
Raphael.js: find the center of element
*/
// element = paper.rect(......);
var cX = element.getBBox().x + element.getBBox().width/2,
cY = element.getBBox().y + element.getBBox().height/2;
.product-page dl{
overflow: hidden;
width:100%;
}
.product-page dt{
width: 30%;
font-weight: bold;
}
.product-page dd{
width: 70%;
/*==============================
BROWSER DETECTION
================================
quick: console.log(/WebKit/.test(navigator.userAgent)) */
function getBrowser(){
var ua = window.navigator.userAgent.toLowerCase();
var ver = window.navigator.appVersion.toLowerCase();
var name = 'unknown';
//==============================
//JAVASCRIPT @MEDIA QUERIES
//================================
var widthMax832px = window.matchMedia('(max-width: 832px)');
console.log('widthMax832px: ', widthMax832px);
//always check if window.matchMedia exist (older IE can crash on this. If still needed, use Paul Irish polyfill)
if(window.matchMedia && window.matchMedia("(max-width: 1201px)").matches) {