how to writing codes one time for diffrent situations?
Hello, please look at this code that I wrote, I have to define screenWidth for two times and also I have to wrote my codes inside and outside window resize.
Is there a better way for this:
html
- <body>
<div class="first">First</div>
<div class="secound">Socound</div>
</body>
jQuery
- $(document).ready(function(){
var screenWidth = $(window).width();
if(screenWidth <= 980){
$('.first').insertAfter('.secound');
}
$(window).on('resize', function(){
var screenWidth = $(window).width();
if(screenWidth <= 980){
$('.first').insertAfter('.secound');
}else{
$('.first').insertBefore('.secound');
}
})
});
demo:
http://jsfiddle.net/sabeti05/z0kkuz70/
thank you