AntiMath » jQuery http://www.antimath.info Simple thoughts blog Sat, 10 Aug 2013 19:41:08 +0000 en-US hourly 1 http://wordpress.org/?v=3.5.1 Quick and simple jQuery accordion http://www.antimath.info/jquery/quick-and-simple-jquery-accordion/ http://www.antimath.info/jquery/quick-and-simple-jquery-accordion/#comments Sat, 10 Aug 2013 19:34:30 +0000 mihai http://www.antimath.info/?p=322 A simple jQuery snippet for those times when you don’t need an entire plugin, but just something quick, and on the spot.

<ul class="accordion">
	<li>
		<div class="heading">Lorem ipsum dolor</div>
		<div class="content">
			Lorem ipsum dolor sit amet...
		</div>
	</li>
</ul>
//accordion
$('.accordion .content').hide();

$('.accordion .heading').click(function(){
	$(this).siblings('.content').slideToggle('fast');
	$(this).parent().toggleClass('active');
	$(this).parent().siblings().children('.content:visible').slideUp('fast');
	$(this).parent().siblings().children('.content:visible').parent().removeClass('active');
});

See demo here.

]]>
http://www.antimath.info/jquery/quick-and-simple-jquery-accordion/feed/ 0
jQuery toggle functions http://www.antimath.info/jquery/jquery-toggle-functions/ http://www.antimath.info/jquery/jquery-toggle-functions/#comments Sun, 24 Mar 2013 13:53:07 +0000 mihai http://www.antimath.info/?p=279 The toggle() method was deprecated in jQuery 1.8 and removed in 1.9.
In jQuery 1.9 the toggle() method does not toggle between two or more functions on a click event, it just toggles the visibility of an element.

I had to find a quick and easy way to toggle between two functions… so here it is:

var x = 0;
$('#toggle-btn').click(function(e){
	if( x == 1 ){
		//console.log('even');
		$(this).removeClass('clicked');
		x = 0;
	} else {
		//console.log('odd');
		$(this).addClass('clicked');
		x = 1;
	}
	e.preventDefault();
});

See demo here.

]]>
http://www.antimath.info/jquery/jquery-toggle-functions/feed/ 0
BxSlider slides counter http://www.antimath.info/jquery/bxslider-slides-counter/ http://www.antimath.info/jquery/bxslider-slides-counter/#comments Sat, 26 Jan 2013 18:21:52 +0000 mihai http://www.antimath.info/?p=271 Add a slides counter ( current/total ) to bxSlider carousels, using bxSlider callbacks and methods.
You just have to add an html element ( ex. ‘<div id=”slide-counter”></div>’ ), where to show the numbers, and the code below will do the rest.

function insertCount(slide_curr,slide_count){
	$('#slide-counter').html('<strong>'+ (slide_curr + 1) +'</strong>/'+ slide_count);
};

var slider = $('#slideshow').bxSlider({
	auto: true,
	pager: false,
	onSliderLoad: function (){
		var slide_count = slider.getSlideCount();
		var slide_curr = slider.getCurrentSlide();
		insertCount(slide_curr,slide_count);
	},
	onSlideNext: function (){
		var slide_count = slider.getSlideCount();
		var slide_curr = slider.getCurrentSlide();
		insertCount(slide_curr,slide_count);
	},
	onSlidePrev: function (){
		var slide_count = slider.getSlideCount();
		var slide_curr = slider.getCurrentSlide();
		insertCount(slide_curr,slide_count);
	}
});

Here you can find a demo.
Note that this is just a rough method, I’m sure it can be done in a more efficient way… maybe when I’ll have more time.

]]>
http://www.antimath.info/jquery/bxslider-slides-counter/feed/ 0
JQuery Cycle pager with leading zeros http://www.antimath.info/jquery/jquery-cycle-pager-leading-zeros/ http://www.antimath.info/jquery/jquery-cycle-pager-leading-zeros/#comments Tue, 22 Jan 2013 20:09:13 +0000 mihai http://www.antimath.info/?p=269 Adding leading zeros to Cycle’s pager can sometimes make a big visual difference. Using the pagerAnchorBuilder this can be achieved very simple:

pagerAnchorBuilder: function(idx, slide) {
	if (idx < 10) {
		return '<li><a href="#">0' + (idx+1) + '</a></li>';
	} else {
		return '<li><a href="#">' + (idx+1) + '</a></li>';
	}
]]>
http://www.antimath.info/jquery/jquery-cycle-pager-leading-zeros/feed/ 0
Custom select input http://www.antimath.info/jquery/custom-select-input/ http://www.antimath.info/jquery/custom-select-input/#comments Tue, 18 Sep 2012 12:33:17 +0000 mihai http://www.antimath.info/?p=228 Using some jQuery and CSS you can make a cross-browser custom select box.

<div class="select-wrapper">
    <select>
        <option selected="selected" value="1">Option 1</option>
        <option value="2">Option 2</option>
        <option value="3">Option 3</option>
        <option value="4">Option 4</option>
        <option value="5">Option 5</option>
    </select>
</div>
.select-wrapper {
  position: relative;
  width: 215px;
  height: 30px;
  padding: 0 25px 0 0;
  margin: 0;
  border: 1px solid #0D5995;
  overflow: hidden;
  background: url(select-arrow.png) no-repeat right center transparent;
}

select {
  position: absolute;
  left: 0;
  top: 0;
  opacity : 0;
  width: 240px;
  height: 30px;
  padding: 5px 0;
  border: none;
  background: transparent !important;
  -webkit-appearance: none;
}

.select-wrapper span {
  display: block;
  width: 210px;
  height: 30px;
  line-height: 30px;
  padding: 0 0 0 5px;
}
$(document).ready(function() {
  $('select').after('<span>' + $('option:selected', this).text() + '</span>');
  
  $('select').click(function(event) {
    $(this).siblings('span').remove();
    $(this).after('<span>' + $('option:selected', this).text() + '</span>');
  });
})

See the demo.

]]>
http://www.antimath.info/jquery/custom-select-input/feed/ 0
jQuery Cycle with carousel thumbs pager http://www.antimath.info/jquery/jquery-cycle-with-carousel-thumbs-pager/ http://www.antimath.info/jquery/jquery-cycle-with-carousel-thumbs-pager/#comments Tue, 28 Feb 2012 16:23:58 +0000 mihai http://www.antimath.info/?p=198 A nice cycle gallery with thumbnail pictures navigation ( like here http://jquery.malsup.com/cycle/pager3.html ), but to use less space, make the thumbnails pager a carousel. ( see a demo here … note, that this is just to prove the concept, not much attention was given to styles ).

If you have “Prev / Next” controls on the cycle slideshow, then you will stumble upon an annoying situation: When you cycle through the slides with the cycle controls, and reach the last visible thumbnail in carousel, you will have to use the carousel controls to go to the current slide’s thumb; to avoid this i used the cycle’s ‘onPrevNextEvent’ callback to navigate through the carousel in the same time you cycle through the slides.

You will need:

1. jQuery

2. Cycle plugin

3. any jquery carousel you want ( i have used jCarousel lite )

Make the markup as you want, and adapt the jQuery code to it.

$('#slideshow').cycle({
    fx: 'scrollHorz',
    speed: 'fast',
    timeout: 0,
    prev: '#prev',
    next: '#next',
    nowrap: 1,
    pager: '#slide-pager ul',
    pagerAnchorBuilder: function(idx, slide) {
        // return selector string for existing anchor
        return '#slide-pager ul li:eq(' + idx + ') a';
    },
    onPrevNextEvent: function(dir, id, el) {
        if (dir === true) {
		if (id >= 3) { //replace '3' with your number of visible elements
			$('#slide-pager_next').click();
		}
        }
        else {
		if (id >= 1) {
			$('#slide-pager_prev').click();
		}
        }
    }
});

$("#slide-pager").jCarouselLite({
    btnNext: "#slide-pager_next",
    btnPrev: "#slide-pager_prev",
    circular: false,
    visible: 3
});

See the demo here.

]]>
http://www.antimath.info/jquery/jquery-cycle-with-carousel-thumbs-pager/feed/ 8
Add repeating CSS selectors http://www.antimath.info/jquery/add-css-selectors-that-repeats/ http://www.antimath.info/jquery/add-css-selectors-that-repeats/#comments Fri, 11 Nov 2011 16:19:15 +0000 mihai http://www.antimath.info/?p=189 Have you ever needed a way to style the elements of a set ( lets say 5 ) individually, but the same rules to be applied to other set ( of 5 ) ?

For example:

<div>
     <h4>first</h4>
     <p>lorem ipsum ...</p>
     <h4>second</h4>
     <h4>third</h4>
     <h4>forth</h4>
     <div>lorem ipsum ...</div>
     <h4>fifth</h4>
     <!-- here end our first set -->
     <h4>first</h4>
     <h4>second</h4>
     <h2>lorem ipsum ...</h2>
     <h4>third</h4>
     <span>lorem ipsum ...</span>
     <h4>forth</h4>
     <h4>fifth</h4>
     <!-- here end our 2nd set -->
</div>

and we want that the first h4 in the first set have same styles as the first h4 in the second and so on, of course our h4′s are not grouped, but they are scattered between other elements, and other h4′s can be added in the future.

If we have a way to distinguish our elements from the other then we can make a short script with jQuery to add classes.

For a group of five css classes that will repeat after a number ( for this example we took, n = 5 ), this means that our h4′s from 1 to 5 will have same classes with the h4′s from 6 to 10, and so on; for this we will use something that in math is called ‘modulo’, basically gives us the remaining of the division of two numbers, and as we can get the index of our element we can see which position has in our set ( in this case of 5 ).

$('div h4').each(function(index) {

	if(index%5 == 0){
		$(this).addClass('first');
	} else if(index%5 == 1){
		$(this).addClass('second');
	} else if(index%5 == 2){
		$(this).addClass('third');
	} else if(index%5 == 3){
		$(this).addClass('forth');
	} else if(index%5 == 4){
		$(this).addClass('fifth');
	}
});

the result would be:

<div>
     <h4 class="first">first</h4>
     <p>lorem ipsum ...</p>
     <h4 class="second">second</h4>
     <h4 class="third">third</h4>
     <h4 class="forth">forth</h4>
     <div>lorem ipsum ...</div>
     <h4 class="fifth">fifth</h4>
     <!-- here end our first set -->
     <h4 class="first">first</h4>
     <h4 class="second">second</h4>
     <h2>lorem ipsum ...</h2>
     <h4 class="third">third</h4>
     <span>lorem ipsum ...</span>
     <h4 class="forth">forth</h4>
     <h4 class="fifth">fifth</h4>
     <!-- here end our 2nd set -->
</div>
]]>
http://www.antimath.info/jquery/add-css-selectors-that-repeats/feed/ 0
Hover effect on the parent of an anchor tag http://www.antimath.info/jquery/hover-effect-on-the-parent-of-an-anchor-tag/ http://www.antimath.info/jquery/hover-effect-on-the-parent-of-an-anchor-tag/#comments Sat, 18 Jun 2011 00:16:49 +0000 mihai http://www.antimath.info/?p=98 It ever happened that you need to apply some CSS style to the parent of a link ( anchor tag <a href=”"></a> ), when the link is hovered ?

For example this HTML:

<div>
   <a href="#">Some Link</a>
</div>

you want to apply some styles to the <div> ( some background color for example ), and you can achieve this by using some ugly css selectors, but sure you will stumble upon some cross-browser problems, so you better use some simple jQuery manipulation.

$('a').mouseover(function (){
	$(this).parent().addClass('hover');
}).mouseout(function (){
	$(this).parent().removeClass('hover');
});

and now you can use the .hover class to style the <div>.

]]>
http://www.antimath.info/jquery/hover-effect-on-the-parent-of-an-anchor-tag/feed/ 0
How to add a class to the current slide in jQuery Cycle http://www.antimath.info/jquery/how-to-add-a-class-to-the-current-slide-in-jquery-cycle/ http://www.antimath.info/jquery/how-to-add-a-class-to-the-current-slide-in-jquery-cycle/#comments Wed, 26 Jan 2011 13:30:01 +0000 mihai http://www.antimath.info/?p=57 I think that every web developer heard about jQuery Cycle plugin, and we all know that is the most simple to use, and the best regarding the options that it has, but that doesn’t mean is bullet proof. In some weird coincidence, I needed a css selector on the current slide ( something like class=”current” ), after I clicked “Inspect element”, I was surprised to find out that Cycle doesn’t have any kind of css selector on the slides ( what looney would need it ) ; so I start searching for a solution,  and that’s how we end up to the snippet of code bellow.

Here is the markup,

<ul id="slideshow">
      <li>
           <img src="path-to-slide1.jpg" width="400" height="300" alt="" />
      </li>
      <li>
           <img src="path-to-slide2.jpg" width="400" height="300" alt="" />
      </li>
     ... etc
 </ul>

and the js,

$('#slideshow').cycle({
     fx: 'fade',
     speed: 'slow',
     timeout: 3000,
     before: function(){
           $(this).parent().find('li.current').removeClass();
     },
      after: function(){
           $(this).addClass('current');
     }
});

I still don’t think this is the best approach to this problem, but it works, and if you know a better way, please leave a comment.

Here you will find a demo, to see it in action.

]]>
http://www.antimath.info/jquery/how-to-add-a-class-to-the-current-slide-in-jquery-cycle/feed/ 6
Checkbox tree with jQuery http://www.antimath.info/jquery/checkbox-tree-with-jquery/ http://www.antimath.info/jquery/checkbox-tree-with-jquery/#comments Thu, 20 Jan 2011 11:45:05 +0000 mihai http://www.antimath.info/?p=54 Update:

With nothing to do, I remembered the failure with the checkbox tree, and I started re-thinking it. With nothing else on my mind, took me very short time to figure my mistakes; …so now i will update this post and the demo. Of course my opinion about checkboxtree jQuery plugin remains the same, but if you don’t want to use an entire plugin, then you can always use the code here.


Sometime ago I needed an “checkbox tree” ( a nested list with checkboxes in every node ), for options that have sub-options. I decided to write one of my own.

After some try & error, I come up with the following code :

Below is the new improved and corrected code.


(function($){

$('ul#checkboxes input[type="checkbox"]').each (
 function () {
  $(this).bind('click change', function (){
   if($(this).is(':checked')) {
    $(this).siblings('ul').find('input[type="checkbox"]').attr('checked', 'checked');
    $(this).parents('ul').siblings('input[type="checkbox"]').attr('checked', 'checked');
   } else {
    $(this).siblings('ul').find('input[type="checkbox"]').removeAttr('checked', 'checked');
   }
  });
 }
);

})(jQuery);

you can see it in action here. (updated demo, with the new code).

I also found this checkboxtree jQuery plugin . I gave it a try, and worked perfectly, and you just have to write:

$('ul#checkboxes').checkboxTree({ });

and you will have a checkboxtree. This plugin has a lot of options, that you will find on the plugin homepage.

I made a simple demo using the plugin, that you can see it here, but you can see a lot more here.

]]>
http://www.antimath.info/jquery/checkbox-tree-with-jquery/feed/ 8
Clear form fields on focus http://www.antimath.info/jquery/clear-form-fields-on-focus/ http://www.antimath.info/jquery/clear-form-fields-on-focus/#comments Fri, 15 Oct 2010 09:59:07 +0000 mihai http://www.antimath.info/?p=37 If you want to make a form without labels for text inputs or textareas, and instead give the fields default values, you will encounter a little annoying  problem, when you focus the field to write something the default value stays there. To make the default value disappear you will need a few lines of javascript.

$('textarea, input:text').bind('focus click', function(){
		if (this.value == this.defaultValue) {
		this.value = '';
		}
	}).bind('blur', function(){
		if (this.value == '') {
		this.value = this.defaultValue;
		}
	});

Note that this script uses jQuery so in order to work you’ll need to have jQuery installed.

To make all clear here is a demo.

]]>
http://www.antimath.info/jquery/clear-form-fields-on-focus/feed/ 1
jQuery title tooltip http://www.antimath.info/jquery/jquery-title-tooltip/ http://www.antimath.info/jquery/jquery-title-tooltip/#comments Fri, 08 Oct 2010 10:33:24 +0000 mihai http://www.antimath.info/?p=25 If you need a simple way to replace the anchor’s ( <a href=”" ></a> ), title=”" attribute with a nice customizable tooltip, below is the code and a demo.

The script adds a html <div> at the end of the <body> tag, and as you hover the specified elements it get’s the title attribute and include it in the <div id=”tooltip”>, the last part of the script is what makes the tooltip move along with mouse cursor.

The nice part is that you can make your tooltip look however you want with CSS.


<ul id="links">
 <li>
   <a href="#" title="This is one">One</a>
 </li>
 <li>
   <a href="#" title="This is two">Two</a>
 </li>
 <li>
   <a href="#" title="This is three">Three</a>
 </li>
 <li>
   <a href="#" title="This is four">Four</a>
 </li>
 <li>
   <a href="#" title="This is five">Five</a>
 </li>
</ul>


#tooltip {
 padding: 10px 15px;
 position: absolute;
 font-size: 14px;
 color: #4E4E4E;
 background: #C4E424;
 z-index: 10;
}


(function($){
	$('body').append('<div id="tooltip"></div>');
	$('#tooltip').hide();
	var $tooltip = $('#tooltip');
    $('ul#links a').each(function(){
        var $this = $(this),
			$title = this.title;
			
        $this.hover(function(){
			this.title = '';
			$tooltip.text($title).show();
        }, function(){
			this.title = $title;
			$tooltip.text('').hide();
        });
		
		$this.mousemove(function(e){
			$tooltip.css({
				top: e.pageY - 10,
				left: e.pageX + 20
			});
		});
    });
})(jQuery);

Here is a demo

]]>
http://www.antimath.info/jquery/jquery-title-tooltip/feed/ 0