5
votes

I'm new to jQuery world, I want to trigger resize when I click on a particular class, I Googled allot, but dint find any solution.

I am working on Highcharts, in which ill add dynamically full-width-box to the parent of panel, when full-width-box is appended to the div I want to trigger the resize.

Here's the code which I was implemented, but dint get the solution

// code for highcharts
$(function () {
    $('.highcharts_horizontal_stacked').highcharts({
        chart: {
            type: 'bar'
        },
        title: {
            text: 'Stacked bar chart'
        },
        xAxis: {
            categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
        },
        yAxis: {
            min: 0,
            title: {
                text: 'Total fruit consumption'
            }
        },
        legend: {
            reversed: true
        },
        plotOptions: {
            series: {
                stacking: 'normal'
            }
        },
        series: [{
            name: 'John',
            data: [5, 3, 4, 7, 2]
        }, {
            name: 'Jane',
            data: [2, 2, 3, 2, 1]
        }, {
            name: 'Joe',
            data: [3, 4, 4, 2, 5]
        }]
    });
});


// for full width function
$('.full-width').click(function () {
  $(this).closest('[class^="col"]').toggleClass('full-width-box');
});


//function for resize
$(".full-width").click(function(e) {
  e.preventDefault();
  setInterval(function() {

    $(window).resize(function() {
      $('body').load( function(){
        $(window).trigger('resize');
      });
    })
  }, 1);
});
.full-width-box{
    width: 100%;
    z-index: 1;
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<!-- Highcharts -->
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<div class="row">
  <div class="col-xs-6">
    <div class="panel panel-default ">
      <div class="panel-heading">
        <h3 class="panel-title">Title</h3>
        <a href="#fullwidth" class="full-width pull-right btn btn-success">
          <i class="glyphicon glyphicon-resize-full"></i>
        </a>
      </div>
      <div class="panel-body">
        <div class="highcharts_horizontal_stacked"></div>
      </div>
    </div>
  </div>
</div>

Here's the JsFiddle Link

2

2 Answers

9
votes

I have found that window.dispatchEvent(new Event('resize')) is the way to trigger the resize.

$('.full-width').click(function () {
  $(this).parents('[class^="col"]').toggleClass('full-width-box');
  window.dispatchEvent(new Event('resize'));
});

Demo http://www.codeply.com/go/qY1TSQOZ0r

P.S. - you don't need the other click/resize function

3
votes

EDIT: Updated code

highcharts().reflow(); might do the trick for you.

$('.full-width').click(function () {      
   $(this).closest('[class^="col"]').toggleClass('full-width-bo‌​x');
   $( ".highcharts" ).each(function( index ) { 
      $(this).highcharts().reflow(); 
   }); 
});

https://jsfiddle.net/remisture/47ghxmc7/5/