Why wavesurferjs is overflowing the parent div - javascript

i have problem with wavesurferjs
It is overflowing the parent div
It is happening for the first time and on resize of the parent div
On resize it should fit the parent div
Question: when parent div is resized waveform should adjust itself to accomodate
it is shown in the below image:
here is my code:
var wavesurfer = WaveSurfer.create({
container: '#waveform',
// waveColor: 'violet',
waveColor: '#5B88C8',
progressColor: '#264E73',
hideScrollbar: true,
cursor: false,
drag: false
});
wavesurfer.load('https://ia800301.us.archive.org/15/items/fire_and_ice_librivox/fire_and_ice_frost_apc_64kb.mp3');
wavesurfer.enableDragSelection({
drag: false,
slop: 1,
loop : false,
});
wavesurfer.on('region-created', function (region) {
console.log(region.start, region.end);
});
wavesurfer.on('ready', function (readyObj) {
wavesurfer.addRegion({
start: 0, // time in seconds
end: wavesurfer.getDuration(), // time in seconds
color: 'hsla(100, 100%, 30%, 0.1)',
loop: false,
multiple: false,
drag: false
});
})
document.querySelectorAll('wave').forEach(function(wave){
wave.addEventListener('mousedown', function(e) {
e.preventDefault();
wavesurfer.clearRegions();
});
});
$('.toggle-width').on('click',function(){
var width = $('#wavesurferContainer').width();
width = width - 120;
$('#wavesurferContainer').width(width + 'px');
});
handle.wavesurfer-handle{
width: 9% !important;
max-width: 7px !important;
/* background: #03A9F4; */
background: orange;
cursor: default !important;
}
#wavesurferContainer{
width: calc(100% - 50px);
border: 1px solid red;
position: relative;
margin-top: 56px;
}
handle.wavesurfer-handle.wavesurfer-handle-end:before{
bottom: -17px !important;
top: unset !important;
}
#waveform{
margin-top: 10%
}
#waveform wave{
overflow: unset !important;
}
span.toggle-width{
position: relative;
float: right;
}
span.toggle-width:before {
content: "<";
position: absolute;
left: 0;
top: 0;
background: red;
width: 30px;
height: 30px;
text-align: center;
line-height: 29px;
color: #fff;
font-size: 24px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.2.3/wavesurfer.min.js"></script>
<!-- wavesurfer.js timeline -->
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.2.3/plugin/wavesurfer.timeline.min.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.1.5/plugin/wavesurfer.regions.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div id="wavesurferContainer">
<span class="toggle-width"></span>
<div id="waveform"></div>
</div>
Please help me thanks in advance!!!

The documentation of wavesurfer mentions the responsive and fillParent options, which should solve the problem.
reponsive is available as of waveSurfer v2.0.0 (source), so an upgrade is needed, in case you are using the version 1.2.3 as in the example snippet.
Latest stable version is 3.3.1.
Edit, as a comment mentions that npm is not in use:
The recent builds of the library can be found in a cdns:
wavesurfer.js/3.3.1/wavesurfer.min.js
wavesurfer.js/3.3.1/plugin/wavesurfer.regions.min.js
Edit2: As documented:
You need to include the plugin's configuration when creating an instance of WaveSurfer:
var wavesurfer = WaveSurfer.create({
container: '#waveform',
plugins: [
WaveSurfer.regions.create({})
]
});
Registering the plugin during the instantiation of wavesurfer solves the problem, as demonstrated in the snippet below.
var wavesurfer = WaveSurfer.create({
container: '#waveform',
// waveColor: 'violet',
waveColor: '#5B88C8',
progressColor: '#264E73',
hideScrollbar: true,
cursor: false,
drag: false,
plugins: [
WaveSurfer.regions.create({})
]
});
wavesurfer.load('https://ia800301.us.archive.org/15/items/fire_and_ice_librivox/fire_and_ice_frost_apc_64kb.mp3');
const resizeObserver = new ResizeObserver(entries => {
for (let entry of entries) {
wavesurfer.empty();
wavesurfer.drawBuffer();
var regs = Object.values(wavesurfer.regions.list);
window.setTimeout(() => {
wavesurfer.regions.clear();
var clear = ({start,end,resize,drag,loop,color}) =>({start,end,resize,drag,loop,color})
regs.forEach(e => wavesurfer.addRegion(clear(e)));
}, 100);
}
});
wavesurfer.enableDragSelection({
drag: false,
slop: 1,
loop : false,
});
wavesurfer.on('region-updated', function (region) {
console.log(region.start, region.end);
});
wavesurfer.on('ready', function (readyObj) {
resizeObserver.observe($('#wavesurferContainer')[0])
wavesurfer.addRegion({
start: 0, // time in seconds
end: wavesurfer.getDuration(), // time in seconds
color: 'hsla(100, 100%, 30%, 0.1)',
loop: false,
multiple: false,
drag: false
});
})
document.querySelectorAll('wave').forEach(function(wave){
wave.addEventListener('mousedown', function(e) {
e.preventDefault();
wavesurfer.clearRegions();
});
});
$(document).on('click','.toggle-width',function(){
console.log('clicked');
var width = $('#wavesurferContainer').width();
width = width - 120;
$('#wavesurferContainer').width(width + 'px');
// you can put here implementation of our redraw.
});
handle.wavesurfer-handle{
width: 9% !important;
max-width: 7px !important;
/* background: #03A9F4; */
background: orange;
cursor: default !important;
}
#wavesurferContainer{
width: calc(100% - 50px);
border: 1px solid red;
position: relative;
margin-top: 56px;
}
handle.wavesurfer-handle.wavesurfer-handle-end:before{
bottom: -17px !important;
top: unset !important;
}
#waveform{
margin-top: 10%
}
#waveform wave{
overflow: unset !important;
}
span.toggle-width{
position: relative;
float: right;
}
span.toggle-width:before {
content: "<";
position: absolute;
left: 0;
top: 0;
background: red;
width: 30px;
height: 30px;
text-align: center;
line-height: 29px;
color: #fff;
font-size: 24px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/3.3.1/wavesurfer.min.js"></script>
<!-- wavesurfer.js timeline -->
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.2.3/plugin/wavesurfer.timeline.min.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/3.3.1/plugin/wavesurfer.regions.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div id="wavesurferContainer">
<span class="toggle-width"></span>
<div id="waveform"></div>
</div>

I found a fix for your current version of wavesurfer.js.
All I did is update drawer size and call redraw method when you change the container width. But then the regions doesn't get updated. So I took the current regions values to a temp variable and removed regions and redraw. Now the region will be in proper position and proper size.
Next issue was that the handle bar keeps moving out of the container and the region starts acting crazy. You can fix it with simple css trick by applying the following css code.
Hope it helps.
handle.wavesurfer-handle-end{
transform: translateX(-100%);
}
Here's a snippet of a working solution.
$('document').ready(function() {
var wavesurfer = WaveSurfer.create({
container: '#waveform',
// waveColor: 'violet',
waveColor: '#5B88C8',
progressColor: '#264E73',
hideScrollbar: true,
cursor: false,
drag: false
});
wavesurfer.load('https://ia800301.us.archive.org/15/items/fire_and_ice_librivox/fire_and_ice_frost_apc_64kb.mp3');
wavesurfer.enableDragSelection({
drag: false,
slop: 1,
loop: false,
});
wavesurfer.on('region-created', function(region) {
console.log(region.start, region.end);
});
wavesurfer.on('ready', function(readyObj) {
wavesurfer.addRegion({
start: 0, // time in seconds
end: wavesurfer.getDuration(), // time in seconds
color: 'hsla(100, 100%, 30%, 0.1)',
loop: false,
multiple: false,
drag: false
});
})
document.querySelectorAll('wave').forEach(function(wave) {
wave.addEventListener('mousedown', function(e) {
e.preventDefault();
wavesurfer.clearRegions();
});
});
$('.toggle-width').on('click', function() {
var width = $('#wavesurferContainer').width();
width = width - 120;
$('#wavesurferContainer').width(width + 'px');
wavesurfer.drawer.updateSize();
wavesurfer.drawBuffer();
var region_list = wavesurfer.regions.list;
var region_keys = Object.keys(region_list);
region_keys.map(function(key){
var temp_region = {
start: region_list[key].start, // time in seconds
end: region_list[key].end, // time in seconds
color: region_list[key].color,
loop: region_list[key].loop,
multiple: region_list[key].multiple,
drag: region_list[key].drag
};
region_list[key].remove();
wavesurfer.addRegion(temp_region);
});
});
});
handle.wavesurfer-handle {
width: 9% !important;
max-width: 7px !important;
/* background: #03A9F4; */
background: orange;
cursor: default !important;
}
handle.wavesurfer-handle-end{
transform: translateX(-100%);
}
#wavesurferContainer {
position: relative;
width: calc(100% - 50px);
border: 1px solid red;
position: relative;
margin-top: 56px;
}
handle.wavesurfer-handle.wavesurfer-handle-end:before {
bottom: -17px !important;
top: unset !important;
}
#waveform {
position: relative;
margin-top: 10%
}
#waveform wave {
overflow: unset !important;
}
span.toggle-width {
position: relative;
float: right;
}
span.toggle-width:before {
content: "<";
position: absolute;
left: 0;
top: 0;
background: red;
width: 30px;
height: 30px;
text-align: center;
line-height: 29px;
color: #fff;
font-size: 24px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.2.3/wavesurfer.min.js">
</script>
<!-- wavesurfer.js timeline -->
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.2.3/plugin/wavesurfer.timeline.min.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.1.5/plugin/wavesurfer.regions.min.js">
</script>
<div id="wavesurferContainer">
<span class="toggle-width">
</span>
<div id="waveform">
</div>
</div>

There is no such functionality in your version of this library, but you still can implement such. Below is working example. Key here is to force to rerender buffers (here drawBuffer()). Regions are separate thing and we just remove and add them manually after redraw (not sure if best idea... but works).
//this needs to be called after resize!
wavesurfer.empty();
wavesurfer.drawBuffer();
var regs = Object.values(wavesurfer.regions.list);
wavesurfer.regions.clear();
regs.forEach(e => wavesurfer.addRegion(e));
this snipped needs to be called whenever container size has been changed. Here we are calling it just after such change from code, but in most cases it will be impossible, then you should use ResizeObserver to handle this.
To keep regions in container add such css:
.wavesurfer-region {
max-width: 100%;
}
var wavesurfer = WaveSurfer.create({
container: '#waveform',
// waveColor: 'violet',
waveColor: '#5B88C8',
progressColor: '#264E73',
hideScrollbar: true,
cursor: false,
drag: false
});
wavesurfer.load('https://ia800301.us.archive.org/15/items/fire_and_ice_librivox/fire_and_ice_frost_apc_64kb.mp3');
const resizeObserver = new ResizeObserver(entries => {
for (let entry of entries) {
wavesurfer.empty();
wavesurfer.drawBuffer();
var regs = Object.values(wavesurfer.regions.list);
window.setTimeout(() => {
wavesurfer.regions.clear();
var clear = ({start,end,resize,drag,loop,color}) =>({start,end,resize,drag,loop,color})
regs.forEach(e => wavesurfer.addRegion(clear(e)));
}, 100);
}
});
wavesurfer.enableDragSelection({
drag: false,
slop: 1,
loop : false,
});
wavesurfer.on('region-updated', function (region) {
console.log(region.start, region.end);
});
wavesurfer.on('ready', function (readyObj) {
resizeObserver.observe($('#wavesurferContainer')[0])
wavesurfer.addRegion({
start: 0, // time in seconds
end: wavesurfer.getDuration(), // time in seconds
color: 'hsla(100, 100%, 30%, 0.1)',
loop: false,
multiple: false,
drag: false
});
})
document.querySelectorAll('wave').forEach(function(wave){
wave.addEventListener('mousedown', function(e) {
e.preventDefault();
wavesurfer.clearRegions();
});
});
$('.toggle-width').on('click',function(){
var width = $('#wavesurferContainer').width();
width = width - 120;
$('#wavesurferContainer').width(width + 'px');
// you can put here implementation of our redraw.
});
handle.wavesurfer-handle{
width: 9% !important;
max-width: 7px !important;
/* background: #03A9F4; */
background: orange;
cursor: default !important;
}
#wavesurferContainer{
width: calc(100% - 50px);
border: 1px solid red;
position: relative;
margin-top: 56px;
}
handle.wavesurfer-handle.wavesurfer-handle-end:before{
bottom: -17px !important;
top: unset !important;
}
#waveform{
margin-top: 10%
}
#waveform wave{
overflow: unset !important;
}
.wavesurfer-region {
max-width: 100%;
}
span.toggle-width{
position: relative;
float: right;
}
span.toggle-width:before {
content: "<";
position: absolute;
left: 0;
top: 0;
background: red;
width: 30px;
height: 30px;
text-align: center;
line-height: 29px;
color: #fff;
font-size: 24px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.2.3/wavesurfer.js"></script>
<!-- wavesurfer.js timeline -->
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.2.3/plugin/wavesurfer.timeline.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.1.5/plugin/wavesurfer.regions.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div id="wavesurferContainer">
<span class="toggle-width"></span>
<div id="waveform"></div>
</div>

Related

JS / CSS issue with Div Slider and Display:Table-Cell

I have a Div slider that rotates a set of Divs in and out of focus. Everything was working fine until I tried switching everything to (table / table-cell) in order to keep them all the Divs the same height in CSS. Now they still rotate out but one div remains visible with a reduced width off to the side of the stage. I get a sense that its position related but just can't figure out what's causing the issue.
Affected Page - https://www.harpercollege.edu/dev/blog-slider-test.php
JS Code:
$('.blog-posts').wrapInner('<div class="blog-posts-stage" />');
// Calculate Conditions & Set Vars
// var playTimer = 9,
slideQty = $('.well').length,
slideWidth = $('.well').width(),
stageWidth = $('.blog-posts-stage').width(),
contWidth = $('.blog-posts').width();
if ((slideQty * slideWidth) < contWidth) {
$('.blog-posts-prev').addClass('blog-posts-prev-disabled').removeClass('blog-posts-prev');
$('.blog-posts-next').addClass('blog-posts-next-disabled').removeClass('blog-posts-next');
} else {
$('.blog-posts-prev-disabled').addClass('blog-posts-prev').removeClass('blog-posts-prev-disabled');
$('.blog-posts-next-disabled').addClass('blog-posts-next').removeClass('blog-posts-next-disabled');
}
$(window).resize(function() {
var slideQty = $('.well').length,
slideWidth = $('.well').width(),
stageWidth = $('.blog-posts-stage').width(),
contWidth = $('.blog-posts').width();
if ((slideQty * slideWidth) < contWidth) {
$('.blog-posts-prev').addClass('blog-posts-prev-disabled').removeClass('blog-posts-prev');
$('.blog-posts-next').addClass('blog-posts-next-disabled').removeClass('blog-posts-next');
} else {
$('.blog-posts-prev-disabled').addClass('blog-posts-prev').removeClass('blog-posts-prev-disabled');
$('.blog-posts-next-disabled').addClass('blog-posts-next').removeClass('blog-posts-next-disabled');
}
});
$('.blog-posts-next').on('click', function(event) {
event.preventDefault();
$('.blog-posts-stage').animate({
left: -(slideWidth)
}, 500, function() {
$('.well:first').appendTo('.blog-posts-stage');
$('.blog-posts-stage').css({
left: '0px'
});
});
});
$('.blog-posts-prev').on('click', function(event) {
event.preventDefault();
$('.well:last').prependTo('.blog-posts-stage');
$('.blog-posts-stage').css({
left: -(slideWidth)
});
$('.blog-posts-stage').animate({
left: '0px'
}, 500, function() {});
});
function moveForward() {
$('.blog-posts-stage').animate({
left: -(slideWidth)
}, 500, function() {
$('.well:first').appendTo('.blog-posts-stage');
$('.blog-posts-stage').css({
left: '0px'
});
});
}
var timer = setInterval(moveForward, playTimer);
$('.blog-posts, .blog-posts-prev, .blog-posts-next').hover(function(ev) {
// clearInterval(timer);
}, function(ev) {
// timer = setInterval( moveForward, playTimer);
});
CSS Code:
<style>
.blog-posts {
width: 100%;
background: #eee;
font-size: 0;
position: relative;
}
.blog-posts-prev,
.blog-posts-next {
display: inline-block;
background: #eee;
color: #000;
text-decoration: none;
padding: 10px;
margin: 5px 0;
}
.blog-posts-prev:hover,
.blog-posts-next:hover {
background: #ccc;
}
.blog-posts-prev-disabled,
.blog-posts-next-disabled {
display: inline-block;
background: #eee;
color: #ccc;
text-decoration: none;
padding: 10px;
margin: 5px 0;
}
.blog-posts-stage {
position: relative;
white-space: normal;
width: 100%;
height: 100%;
float: none;
}
.well {
background: #ccc;
box-shadow: inset -1px 0px 0px 0px rgb(255, 255, 255);
width: 100%;
font-size: 12px;
text-align: left;
display: table-cell;
height: 100%;
width: 100%;
}
.well .row .col-sm-12.col-md-12 h2 {
float: left;
margin-top: 0px;
}
</style>
You could just use a lightbox library and save yourself the effort, but if you really want to do this why not try flex?
.blog-posts-stage {
display: flex;
flex-direction: row;
overflow: hidden;
}
.well-large {
flex-shrink: 0;
}

Jquery effect function call not working in javascript

I have a code which displays congradulations and displays stars popping out when body loads in a blue screen for for a timeout of 4000
when body loads it calls a function animatecongrat() which has two functions in it given below
The text congradulations is animated in function animatetext(), and stars animated in animateblobs()
My problem is text congradulation is animated and bluescreen appears for a timeout of 4000 but stars ( var numberOfStars = 20; in blob) are not appearing and animated in blue screen.
How to solve this? How do i achieve this effect?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.0/TweenMax.min.js'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js'></script>
<script>
var timeOut;
function animateCongrat()
{
debugger;
$('.congrats').show();
clearTimeout(timeOut);
addBlueBody();
reset();
var numberOfStars = 20;
for (var i = 0; i < numberOfStars; i++) {
$('.congrats').append('<div class="blob fa fa-star ' + i + '"></div>');
}
animateText();
animateBlobs();
hideCongratAndBlueBody();
}
function addBlueBody() {
$('body').addClass('bodyblue');
$('#2').hide();
$('.hiddenimage').show();
}
function hideCongratAndBlueBody() {
timeOut = setTimeout(() => {
$('.congrats').hide();
$('body').removeClass('bodyblue');
}, 4000);
}
function reset() {
$.each($('.blob'), function(i) {
TweenMax.set($(this), {
x: 0,
y: 0,
opacity: 1
});
});
TweenMax.set($('h1'), {
scale: 1,
opacity: 1,
rotation: 0
});
}
function animateText() {
TweenMax.from($('h1'), 0.8, {
scale: 0.4,
opacity: 0,
rotation: 15,
ease: Back.easeOut.config(4),
});
}
function animateBlobs() {
debugger;
var xSeed = _.random(500, 500);
debugger;
var ySeed = _.random(300, 300);
$.each($('.blob'), function(i) {
var $blob = $(this);
var speed = _.random(1, 5);
var rotation = _.random(5, 100);
var scale = _.random(0.8, 1.5);
var x = _.random(-xSeed, xSeed);
var y = _.random(-ySeed, ySeed);
TweenMax.to($blob, speed, {
x: x,
y: y,
ease: Power1.easeOut,
opacity: 0,
rotation: rotation,
scale: scale,
onStartParams: [$blob],
onStart: function($element) {
$element.css('display', 'block');
},
onCompleteParams: [$blob],
onComplete: function($element) {
$element.css('display', 'none');
}
});
});
}
#font-face {
font-family: 'Sigmar One';
font-style: normal;
font-weight: 400;
src: local('Sigmar One Regular'), local('SigmarOne-Regular'), url(https: //fonts.gstatic.com/s/sigmarone/v8/co3DmWZ8kjZuErj9Ta3do6Tpow.ttf) format('truetype');
}
#import url(https: //fonts.googleapis.com/css?family=Sigmar+One);
body {
overflow: hidden;
}
.dashed {
border: 2px dashed #999 !important;
}
.bodyblue {
background: #3da1d1;
color: #fff;
width: 0.3vw;
height: 0.5vh;
}
.congrats {
position: absolute;
top: 140px;
width: 550px;
height: 100px;
padding: 20px 10px;
text-align: center;
margin: 0 auto;
left: 0;
right: 0;
display: none;
}
h1 {
transform-origin: 50% 50%;
font-size: 50px;
font-family: 'Sigmar One', cursive;
cursor: pointer;
z-index: 2;
text-align: center;
width: 100%;
position: absolute;
top:-10.5vh;
left: 0.3vw;
}
.blob {
height: 50px;
width: 50px;
color: #ffcc00;
position: absolute;
top: 45%;
left: 45%;
z-index: 1;
font-size: 30px;
display: none;
}
.heading{
margin-left:20%;
margin-right:20%;
margin-top:-2%;
}
.text {
padding: 20px;
margin:7 px;
margin-top:10px;
color:white;
font-weight:bold;
text-align:center;
}
body{
background-image:
background-size: 100vw 100vh;
}
.next{
margin-right:50%;
margin-left:50%;
margin-bottom:10%;
float:right;
}
ul{
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body onload="animateCongrat();">
<div class="congrats">
<h1>Congratulations!</h1>
</div>
</body>

Add DragScroll to slider

¡Hello! I have a slider of 3 sections, which works automatic and onlick, everything is fine but i want to add a dragscroll for mobile, but i don't have a clue of where to start, heres the code
//almacenar slider en una variable
var slider = $('#slider');
//almacenar botones
var siguiente = $('#btn-next');
var anterior = $('#btn-prev');
//mover ultima imagen al primer lugar
$('#slider section:last').insertBefore('#slider section:first');
//mostrar la primera imagen con margen de -100%
slider.css('margin-left', '-'+100+'%');
function moverD() {
slider.animate({marginLeft:'-'+200+'%'}, 700, function(){
$('#slider section:first').insertAfter('#slider section:last');
slider.css('margin-left', '-'+100+'%');
});
}
function moverI() {
slider.animate({marginLeft:0}, 700, function(){
$('#slider section:last').insertBefore('#slider section:first');
slider.css('margin-left', '-'+100+'%');
});
}
function autoplay() {
interval = setInterval(function(){
moverD();
}, 5000);
}
siguiente.on('click',function() {
moverD();
clearInterval(interval);
autoplay();
});
anterior.on('click',function() {
moverI();
clearInterval(interval);
autoplay();
});
autoplay();
#principal{
position: relative;
height: 300px;
width: 300px;
overflow: hidden;
border-bottom: 6px solid #80d443;
border-top: 6px solid #80d443;
}
#btn-prev, #btn-next{
position: absolute;
text-shadow: 2px 2px 1px black;
cursor: pointer;
color: white;
font-size: 30px;
z-index: 80;
top: 50%;
font-weight: bold;
}
#btn-prev{
left: 1%;
}
#btn-next{
right: 1%;
}
#slider{
display: flex;
width: 900px;
height: 300px;
}
section{
position: relative;
margin: 0 auto;
width: 100%;
height: 100%;
}
#diseño{
background: blue;
}
#solucion{
background: red;
}
#entrenamiento{
background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="content" id="principal">
<section class="slider" id="slider">
<section id="diseño">
</section>
<section id="solucion">
</section>
<section id="entrenamiento">
</section>
</section>
<div id="btn-prev" class="fa fa-angle-left" aria-hidden="true" data-stellar-ratio="1"><</div>
<div id="btn-next" class="fa fa-angle-right" aria-hidden="true" data-stellar-ratio="1">></div>
</section>
Any example or hint would be useful, Thanks in advance :D
I tried something like this and there i stopped
$(function() {
var slides = $('#slider section').length;
var slideWidth = $('#slider').width();
var min = 0;
var max = -((slides - 1) * slideWidth);
$("#slider").width(slides*slideWidth).draggable({
axis: 'x',
drag: function (event, ui) {
if (ui.position.left > min) ui.position.left = min;
if (ui.position.left < max) ui.position.left = max;
}
});
});
EDIT:
I manage to make it work the drag function, but i'm having a lot of problems, here you can see the DEMO https://jsfiddle.net/visiond/9j3jLann/8/
I am adding a second answer in case either option suits the OP better or those in the future.
This is using jquery and slick.js
HTML
<section class="slider">
<div><img src="http://placeholder.of.today/300x300/12ec14/1844d"></div>
<div ><img src="http://placeholder.of.today/300x300/ff0000/1844d"></div>
<div ><img src="http://placeholder.of.today/300x300/0095ff/1844d"></div>
</section>
CSS
.slider {
max-width: 300px;
margin: 0 auto;
}
.slick-slide {
color: white;
padding: 0;
margin: 0;
text-align: center;
img { display: inline-block; }
}
.slick-prev:before,
.slick-next:before {
color: black;
}
.slick-prev, .slick-next {
font-size: 0;
line-height: 0;
position: absolute;
top: 50%;
display: block;
width: 20px;
height: 20px;
z-index: 3;
}
.slick-next {
right: 5px;
}
.slick-prev {
left: 5px;
}
JS
$(".slider").slick({
autoplay: true,
dots: true,
customPaging : function(slider, i) {
},
responsive: [{
breakpoint: 500,
settings: {
dots: false,
arrows: false,
infinite: false,
slidesToShow: 1,
slidesToScroll: 1
}
}]
});
DEMO on Codepen.
https://codepen.io/norcaljohnny/pen/NbpPvL
You are very close.
$(function() {
var slides = $('#slider ul').children().length;
var slideWidth = $('#slider').width();
var min = 0;
var max = -((slides - 1) * slideWidth);
$("#slider ul").width(slides*slideWidth).draggable({
axis: 'x',
drag: function (event, ui) {
if (ui.position.left > min) ui.position.left = min;
if (ui.position.left < max) ui.position.left = max;
}
});
});
DEMO http://jsfiddle.net/norcaljohnny/k71jugLk/

.stop on .mouseover or .hover

I can't figure out how to stop and resume the slide on a mouseover or hover occurrence. I basically want to stop all the scripts when .mouseover or .hover is triggered. Can anyone help me on this?
Edit: Code should work if you simply copy paste it, it is all hosted externally
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.2.min.js" type="text/javascript"></script>
<script src="http://static.tumblr.com/jvojroo/DIamwjvp3/jquery.caroufredsel-6.2.0-packed.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$('#slider').carouFredSel({
width: '100%',
align: false,
items: 4,
items: {
width: $('#wrapper').width() * 0.15,
height: 500,
visible: 1,
minimum: 1
},
scroll: {
items: 1,
timeoutDuration: 1000,
onBefore: function(data) {
// find current and next slide
var currentSlide = $('.slide.active', this),
nextSlide = data.items.visible,
_width = $('#wrapper').width();
// resize currentslide to small version
currentSlide.stop().animate({
width: _width * 0.15
});
currentSlide.removeClass('active');
// hide current block
data.items.old.add(data.items.visible).find('.slide-block').stop().fadeOut();
// animate clicked slide to large size
nextSlide.addClass('active');
nextSlide.stop().animate({
width: _width * 0.7
});
},
onAfter: function(data) {
// show active slide block
data.items.visible.last().find('.slide-block').stop().fadeIn();
}
},
onCreate: function(data) {
// clone images for better sliding and insert them dynamacly in slider
var newitems = $('.slide', this).clone(true),
_width = $('#wrapper').width();
$(this).trigger('insertItem', [newitems, newitems.length, false]);
// show images
$('.slide', this).fadeIn();
$('.slide:first-child', this).addClass('active');
$('.slide', this).width(_width * 0.15);
// enlarge first slide
$('.slide:first-child', this).animate({
width: _width * 0.7
});
// show first title block and hide the rest
$(this).find('.slide-block').hide();
$(this).find('.slide.active .slide-block').stop().fadeIn();
}
});
// Handle click events
$('#slider').children().click(function() {
$('#slider').trigger('slideTo', [this]);
});
$('.slide:firstchild').mouseover(function() {
$('.slide:firstchild').stop();
});
$('#slider').children().mouseover(function() {
$('#slider').stop();
});
//$('#slider').children().mouseout(function() {
// $('#slider').trigger( 'slideTo', [this] );
//});
// Enable code below if you want to support browser resizing
$(window).resize(function() {
var slider = $('#slider'),
_width = $('#wrapper').width();
// show images
slider.find('.slide').width(_width * 0.15);
// enlarge first slide
slider.find('.slide.active').width(_width * 0.7);
// update item width config
slider.trigger('configuration', ['items.width', _width * 0.15]);
});
});
</script>
<style type="text/css">
html, body {
height: 100%;
padding: 0;
margin: 0;
}
body {
background: #f9f9f3;
}
body * {
font-family: Arial, Geneva, SunSans-Regular, sans-serif;
font-size: 14px;
color: #222;
line-height: 20px;
}
#wrapper {
height: 100%;
width: 100%;
min-height: 650px;
min-width: 900px;
padding-top: 1px;
}
#slider {
margin: 100px 0 0 0;
height: 500px;
overflow: hidden;
}
#slider .slide {
position: relative;
display: none;
height: 500px;
float: left;
background-position: center right;
cursor: pointer;
border-left: 1px solid #fff;
}
#slider .slide:first-child {
border: none;
}
#slider .slide.active {
cursor: default;
}
#slider .slide-block {
position: absolute;
left: 40px;
bottom: 75px;
display: inline-block;
width: 435px;
background-color: #fff;
background-color: rgba(255, 255, 255, 0.8);
padding: 20px;
font-size: 14px;
color: #134B94;
border: 1px solid #fff;
overflow: hidden;
border-radius: 4px;
}
#slider .slide-block h4 {
font-size: 36px;
font-weight: bold;
margin: 0 0 10px 0;
line-height: 1;
}
#slider .slide-block p {
margin: 0;
}
#donate-spacer {
height: 0;
}
#donate {
border-top: 1px solid #999;
width: 750px;
padding: 50px 75px;
margin: 0 auto;
overflow: hidden;
}
#donate p, #donate form {
margin: 0;
float: left;
}
#donate p {
width: 650px;
color: #999;
}
#donate form {
width: 100px;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="slider">
<div class="slide">
<img src="http://farm4.staticflickr.com/3821/10956569263_92a647e267_o.png">
<div class="slide-block">
<h4>Ice Age</h4>
<p>Heading south to avoid a bad case of global frostbite, a group of migrating misfit creatures embark on a hilarious quest to reunite a human baby with his tribe.</p>
</div>
</div>
<div class="slide">
<img src="http://farm8.staticflickr.com/7444/10956575693_94fd773731_o.png">
<div class="slide-block">
<h4>For The Birds</h4>
<p>For the Birds is an animated short film, produced by Pixar Animation Studios released in 2000. It is shown in a theatrical release of the 2001 Pixar feature film Monsters, Inc.</p>
</div>
</div>
<div class="slide">
<img src="http://farm4.staticflickr.com/3789/10956504824_4845433ff6_o.png">
<div class="slide-block">
<h4>UP</h4>
<p>A comedy adventure in which 78-year-old Carl Fredricksen fulfills his dream of a great adventure when he ties thousands of balloons to his house and flies away to the wilds of South America.</p>
</div>
</div>
<div class="slide">
<img src="http://farm6.staticflickr.com/5464/9449526762_ed5339251e_o.jpg">
<div class="slide-block">
<h4>Ice Age</h4>
<p>Heading south to avoid a bad case of global frostbite, a group of migrating misfit creatures embark on a hilarious quest to reunite a human baby with his tribe.</p>
</div>
</div>
</div>
</div>
</body>
</html>
You can trigger a custom event named "stop" on carouFredSel component If you want to stop the slider.
$('#slider').trigger("stop");
And trigger a custom event named "play" with a extra parameter true to resume the slider
$("#slider").trigger("play",true);
For example:
<script>
$(function(){
$("#slider").carouFredSel({
items: 4
});
$("#slider > div.slide").hover(
function(){
$("#slider").trigger("stop");
},
function(){
$("#slider").trigger("play",true);
}
);
});
</script>
Hope this is helpful for you.

Why is this animation choppy and sometimes have long delay before it executes?

This jquery animation is very choppy in firefox and in both firefox and chrome it frequently has a noticeable delay (~1 second) before it actually starts animating (if I put code to write out tot he console when the onclick handler is called, that will show up immeadiately, but the animation call will have a delay). This delay is not every time, but if you click maybe 5-6 times, you'll see it at least once.
<!doctype html>
<html>
<head>
<title>Test Lag</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<style>
.base
{
position: absolute;
top: 507px;
left: 0px;
width: 1000px;
height: 40px;
z-index: 0;
}
.one
{
position: absolute;
top: 2px;
left: 2px;
width: 994px;
height: 34px;
background-color: #ffffff;
border: solid 1px #505050;
z-index: 3;
opacity: 0.5;
filter: alpha(opacity=50);
}
.oneA
{
position: absolute;
top: 0px;
left: 0px;
width: 966px;
height: 6px;
margin: 10px;
background-color: #999999;
border: solid #cccccc 4px;
}
.two
{
position: absolute;
top: 1px;
left: 1px;
width: 996px;
height: 36px;
background-color: #e8e8e8;
border: solid 1px #505050;
z-index: 2;
opacity: 0.25;
filter: alpha(opacity=25);
}
.three
{
position: absolute;
top: 0px;
left: 0px;
width: 998px;
height: 38px;
background-color: #e8e8e8;
border: solid 1px #505050;
z-index: 1;
opacity: 0.12;
filter: alpha(opacity=12);
}
.four
{
position: absolute;
top: 17px;
left: 17px;
width: 966px;
height: 6px;
background-color: #e8e8e8;
z-index: 0;
opacity: 0.5;
filter: alpha(opacity=50);
}
.five
{
position: absolute;
top: 17px;
left: 17px;
width: 966px;
height: 366px;
z-index: 4;
overflow: hidden;
}
</style>
</head>
<body>
<div id ="base" class="base">
<div id="one" class="one">
<div id="oneA" class="oneA"></div>
</div>
<div id="two" class="two"></div>
<div id="three" class="three"></div>
<div id="four" class="four"></div>
<div id="five" class="five">There's some text in here.</div>
</div>
<script>
var isOn = false;
var jq_base = $('#base');
var jq_one = $('#one');
var jq_oneA = $('#oneA');
var jq_two = $('#two');
var jq_three = $('#three');
var jq_four = $('#four');
var baseTop = 96;
var baseStartTop =507;
var baseHeight = 400;
var oneHeight = 394;
var oneAHeight = 366;
var twoHeight = 396;
var threeHeight = 398;
var fourHeight = 366;
var baseStartH = 40;
var oneStartH = 34;
var oneAStartH = 6;
var twoStartH = 36;
var threeStartH = 38;
var fourStartH = 6
document.onclick = function()
{
//It's opened
if ( isOn )
{
jq_one.animate( { height: oneStartH }, { duration: 300, queue: false } );
jq_oneA.animate( { height: oneAStartH }, { duration: 300, queue: false } );
jq_two.animate( { height: twoStartH }, { duration: 300, queue: false } );
jq_three.animate( { height: threeStartH }, { duration: 300, queue: false } );
jq_four.animate( { height: fourStartH }, { duration: 300, queue: false } );
jq_base.animate(
{ height: baseStartH },
{
duration: 300,
queue: false,
step: function (now)
{
if ( now <= ( baseStartH + 10 ) ) jq_base.animate( { top: baseStartTop }, 800 );
}
}
);
isOn = false;
}
//It's closed
else
{
jq_base.animate(
{ top: baseTop },
{
duration: 800,
step: function (now)
{
if ( now <= 100 )
{
jq_base.animate( { height: baseHeight }, { duration: 300, queue: false } );
jq_one.animate( { height: oneHeight }, { duration: 300, queue: false } );
jq_oneA.animate( { height: oneAHeight }, { duration: 300, queue: false } );
jq_two.animate( { height: twoHeight }, { duration: 300, queue: false } );
jq_three.animate( { height: threeHeight }, { duration: 300, queue: false } );
jq_four.animate( { height: fourHeight }, { duration: 300, queue: false } );
}
}
}
);
isOn = true;
}
}
</script>
</body>
</html>
DEMO: http://jsfiddle.net/fnswz/
I think the problem is in the following part of the if (isOn) branch:
jq_base.animate(
{ height: baseStartH },
{
duration: 300,
queue: false,
step: function (now)
{
if ( now <= ( baseStartH + 10 ) )
jq_base.animate( { top: baseStartTop }, 800 );
}
}
);
Specifically, within the step function of that particular animate() call you start another animation with quite a long duration (800ms), and because it is in the step, even with the if test you are starting multiple animations with that long duration.
That means that although the process reaches a point where it looks finished because everything has moved to its final position, behind the scenes these extra animations haven't finished running and so it doesn't respond properly to subsequent clicks. If you move that 800ms animation out of the step function and simple put it afterwards it seems to make everything work much more smoothly. (At least, it seems much better in Chrome: as I said in my comment above I don't have FF on this computer so I can't test that.)
Here's a demo with the change I'm talking about: http://jsfiddle.net/fnswz/1/
You may want to set a flag animationRunning when you start the animation and then unset it using animate()'s complete callback, so that you can test the flag and ignore clicks until the current animation has finished, but just the change above made a big improvement for me.
(By the way, why are you using document.onclick = ... when you're using jQuery?)

Categories

Resources