Follow Grid

i have a 4x4 block that i want to follow where the mouse is but constrained to a grid. each block in the grid is 32x32 pxs. heres code i have:

var distY = Math.sqrt((this.y-b.game.mouseLayer.mouseY)*(this.y-b.game.mouseLayer.mouseY));
			var distX = Math.sqrt((this.x-b.game.mouseLayer.mouseX)*(this.x-b.game.mouseLayer.mouseX));
			
			if(distX>34){
				if(this.x<b.game.mouseX){
					//b.fixed = false;
					b.x += 17;
					//b.fixed = true;
				}else{
					//b.fixed = false;
					b.x -= 17;
					//b.fixed = true;
				}
				
			}
			
			if(distY>34){
				if(this.y<b.game.mouseY){
					//b.fixed = false;
					b.y += 17;
					//b.fixed = true;
				}else{
					//b.fixed = false;
					b.y -= 17;
					//b.fixed = true;
				}
			}

but this doesnt work. i can sometimes get it to get onto the grid then if i move my mouse slowly to an outside distance it will then snap and constrain to grid like it should but shouldnt this code make it just work and follow mouse?