ReferenceError: Velocity is not defined - javascript
Looking for a resolution for this issue. I set up velocity on wordpress and seem to have run into a wall. When I reference velocity, I get Velocity not defined, and when I reference $.Velocity, I get $ not defined. Jquery 1.11.3 and Velocity are installed but the script that I am running on velocity does not call any Jquery items.
Any help would be much appreciated.
// Velocity.js instead of CSS for performance
var city = document.querySelector('.js-city-1');
var fulllogo = document.querySelector('.cityanimation');
var loading = [animatecity()];
function animatecity() {
// Reset
Velocity(city, {
'stroke-dasharray': 3542,
'stroke-dashoffset': 3542
}, 0);
// Animate
Velocity(city, {
'stroke-dashoffset': 0
}, {
duration: 20000,
complete: function() {
Velocity(city, {
opacity: 0
}, {
duration: 500
}), animatefulllogo();
}
,
});
}
function animatefulllogo() {
// Reset
Velocity(fulllogo, {
opacity: 1
}, 0);
// Animate
Velocity(fulllogo, {
opacity: 0
}, {
duration: 200,
complete: function() {;
}
});
};
Velocity.RunSequence(loading);
#loadscreen{
z-index: 9999;
width: 100%;
height: 100%;
overflow: hidden;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
position: fixed;
background-color: #FFF;
}
#loaderimage{
background-image: url(/wp-content/themes/silicon-city/images/SiliconCity.jpg);
background-position: 50% 50%;
background-repeat: no-repeat;
-webkit-background-size: 100%;
-ms-background-size: 100%;
background-size: 100%;
max-width:1035px;
min-width:360px;
position: fixed;
}
#Layer_1{
margin-top:8px;
}
.cityanimation {
background-color: #FFF;
top:50%;
left:50%;
width:75%;
max-width:1035px;
min-width:360px;
position: fixed;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%); /* IE 9 */
-webkit-transform: translate(-50%, -50%); /* Safari */
}
.st0{
fill:none;
stroke:#28B24B;
stroke-width:.15em;
stroke-linecap:round;
stroke-linejoin:round;
stroke-miterlimit:10;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.min.js"></script>
<div id="loadscreen">
<div id="loaderimage">
<div class="cityanimation">
<object>
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink= "http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 1035 393" style="enable-background:new 0 0 1035 393;" xml:space="preserve">
<path class="st0 js-city-1" d="M1.521,213.364h59.398l6.658-0.488v-57.018h15.737v-6.528l10.905-0.134v5.685h17.706l0.122,3.899h1.178h7.636
v23.01l6.009,0.057v-3.574h5.524v4.145h12.426l0.082-4.956h6.822v2.597h5.685V139.75h4.009v-8.015h1.649v-4.713h1.415v-4.716h1.18
v-6.363h1.649v6.363h0.942v4.244h1.887v5.422h11.551v3.065l13.673,13.674v28.289l16.138-0.351v-24.526h2.438v-7.961h32.489v25.018
h4.873v-23.396h13.646v12.35h19.492v18.191h3.574v11.35l16.245,0.027v-35.578h29.727v11.914h10.942l0.268-34.333h19.33v28.102h9.746
l1.625,37.933h8.937v-17.652l42.396-1.598V158.78h13.807l2.763-112.293h2.436v-3.899h5.684v-8.936h-1.136v-2.437h1.623V2.139
L453.054,1l0.49,0.976v29.239h1.786v2.437h-1.299v8.771h5.847v3.899h2.926l2.422,136.264h8.765l0.183-10.971h4.063v-2.762
c0,0,7.389-6.746,13.97,0l1.625-6.498l8.284-5.847l7.637,5.197l2.596,5.196h4.225v-8.932h18.844v23.555h9.096v15.568l23.398-0.359
v-24.518h5.463v-5.809h6.764v-25.592h24.609v36.268h27.301v-17.017h9.902v-7.386h9.695v3.806h16.051v3.136h13.098v3.806h3.158
v-14.778h4.408l1.732-19.33l2.691,19.371h3.584v18.543h4.215v5.15h10.146v14.332h7.527v-12.093h2.74V141.26h2.951v-5.151h12.334
v36.05h3.795v22.393h5.609v-17.914h8.773l2.316-0.006v24.859h14.514h2.529v-15.662h3.939v-13.444h3.775V88.216h2.713v-6.998h4.416
v-6.996h1.988c0.1-0.31,0.209-0.6,0.285-0.898c1.094-4.186,1.883-8.421,1.992-12.759c0.029-1.136,0.055-2.275-0.025-3.406
c-0.072-0.959,0.209-1.709,0.934-2.299c0.225-0.181,0.289-0.365,0.289-0.636c-0.008-4.522,0.037-9.042-0.027-13.56l0.049-0.999
c-0.016,4.854-0.006,9.711-0.014,14.57c0,0.299,0.035,0.49,0.367,0.642c0.652,0.3,1.113,0.844,1.24,1.553
c0.086,0.497,0,1.021,0.004,1.533c0.006,1.682-0.076,3.372,0.059,5.042c0.162,1.95,0.451,3.896,0.809,5.823
c0.336,1.814,0.828,3.597,1.25,5.394c0.055,0.239,0.174,0.334,0.436,0.324c0.541-0.024,1.086-0.007,1.674-0.007v7.019h4.143v6.982
h2.924v84.164h3.479v8.4h4.441l4.865,0.002v-16.695v-12.764h7.533v-3.808h13.012v-9.853h5.395v15.004h9.057v-18.136h17.057v10.075
h12.141v-4.702h4.238v4.477h5.395v-8.507h10.02v9.18h4.049v19.928h7.23v7.611h7.6v-17.018h1.732v-3.58h10.404v43.213h10.994v-86.428
h2.311v-3.357h2.699v-3.582h16.752v4.029h2.508v3.583h1.926v83.945h5.852l-0.031-56.667l0.678-5.362l-1.469-0.789l0.057-0.396h1.525
l0.111-16.925l0.336-1.243l1.297-0.395l0.287-3.723l0.732-1.581c-0.074-1.429-0.574-2.858,0.619-4.289
c0.26-1.902-0.264-4.195,1.355-5.417c0.27-2.018-0.154-4.479,1.639-5.528c0.332-1.789-0.354-3.573,1.07-5.36
c0.236-2.353,0.145-4.705,1.072-7.053l0.336-5.304l0.283-19.241l0.678,18.279l0.678,6.827c0.838,2.051,0.635,4.103,0.789,6.151
c1.395,1.619,0.92,3.236,0.902,4.855c2.518,1.938,1.785,4.069,1.92,6.149c1.561,0.804,1.262,3.283,1.41,5.36
c1.109,1.447,0.959,2.898,0.902,4.344c0.451,1.581,0.695,3.16,0.848,4.74l1.352,1.129l0.451,5.076l0.115,13.262l0.676,0.563
l0.678-0.506l0.395,0.111l-0.057,0.508l-1.354,0.677v5.643l0.508,0.339l0.557,50.658h8.58v-11.014h19.199l-0.006,0.919v34.813
l7.383,1.607l-14.375,0.805l14.375,2.41l-15.15,1.205l15.924,2.412l-14.76,1.607l6.604,1.205v57.908h-15.236h-8.654h-33.711"/>
</svg>
</object>
</div>
</div>
</div>
I also tried the following... thought it might be worth a shot..
jQuery.noConflict();
(function( $ ) {
$(function() {
// Velocity.min.js instead of CSS for performance
var $city = $(".js-city"),
$fulllogo = $(".cityanimation");
var loading = [animatecity()];
function animatecity() {
// Reset
$city
.velocity({
'stroke-dasharray': 3542,
'stroke-dashoffset': 3542
}, 0);
// Animate
$city
.velocity({
'stroke-dashoffset': 0
}, {
duration: 20000,
complete: function() {
$city
.velocity({
opacity: 0
}, {
duration: 500
}), animatefulllogo();
}
,
});
}
function animatefulllogo() {
// Reset
$fulllogo
.velocity({
opacity: 1
}, 0);
// Animate
$fulllogo
.velocity({
opacity: 0
}, {
duration: 200,
complete: function() {;
}
});
};
animatecity();
});
})(jQuery);
Okay... ended up with this.. but looks like Velocity.js doesn't load on window. I did confirm to see if Velocity.js was in the correct directory and it is. The animation still does not work but I am not getting any errors..
(function($) {
// Velocity.min.js instead of CSS for performance
var city = document.querySelector('.js-city');
var fulllogo = document.querySelector('.cityanimation');
function animatecity() {
// Reset
$.Velocity(city, {
'stroke-dasharray': 3542,
'stroke-dashoffset': 3542
}, 0);
// Animate
$.Velocity(city, {
'stroke-dashoffset': 0
}, {
duration: 20000,
complete: function() {
$.Velocity(city, {
opacity: 0
}, {
duration: 500
}), animatefulllogo();
}
,
});
}
function animatefulllogo() {
// Reset
$.Velocity(fulllogo, {
opacity: 1
}, 0);
// Animate
$.Velocity(fulllogo, {
opacity: 0
}, {
duration: 200,
complete: function() {;
}
});
};
animatecity();
})(jQuery);
You need to declare jquery before velocity in your markup scripts
You can see velocity dependence package depends on jquery 1.4 version or more, so it is being dependent on jquery for something.
The reason is that word press actually defines jQuery as jQuery not $. If you run this code:
jquery(document).ready(function () {
var $ = jQuery;
console.log('Now jquery is $', $);
});
So you should have source code jQuery and then asign to $ and finally source code of velocity.
Related
How to save object position after animation in JavaScript?
I am trying to animate an object for a game in which the ball is supposed to move up, right, left and down on pressing user button. I am trying to achieve this by using .animate() function on the ball. But after every animation event, the ball resets to its initial position and does not continue moving from its "new" position. Is there any way to accomplish this? Following is my condensed code snippet for simplicity: /* Animation */ var item = document.getElementById('item'); var anim; function myMoveLeft(){ anim=item.animate([ // keyframes { transform: 'translateX(0px)' }, { transform: 'translateX(-60px)' } ], { duration: 1000, iterations: 1 }); } function myMoveDown(){ anim=item.animate([ // keyframes { transform: 'translateY(0px)' }, { transform: 'translateY(60px)' } ], { duration: 1000, iterations: 1 }); // anim.onfinish(()=>{console.log("onfinish ran")}) } item.addEventListener('animationend', () => { console.log('Animation ended'); }); button{ display:inline-block; height:40px; width:80px; } #myContainer { width: 400px; height: 400px; position: relative; background: lightgray ; } #item { background: orange; position: absolute; right:30px; top:30px; height: 30px; width: 30px; border-radius:50%; } <p> <button onclick="myMoveLeft()">Left</button> <button onclick="myMoveDown()">Down</button> </p> <div id ="myContainer"> <div id="item"></div> </div> As seen, I have already tried using .onfinish() and Event Listener 'animationend' hoping I could update the new 'right' and 'top' position but it does not work. Not sure if that would be the right approach. Could someone please suggest on how to save the element to a new position and animate it further from that new position? PS: I am also open to suggestions/techniques if you feel there are other better ways to do this. Thanks a lot in Advance!!
You can make the ball get the final transform value using the fill option, which is the same as animation-fill-mode in css animation. For not override the transform when you do the next animation, you can save the x and y value as variables, and do any animation according to the current x and y state. (from x to x-60, instead from 0 to -60, etc.) Example: /* Animation */ var item = document.getElementById('item'); var anim; var x=0, y=0; function myMoveLeft(){ anim=item.animate([ // keyframes { transform: `translate(${x}px, ${y}px)` }, { transform: `translate(${x-60}px, ${y}px)` } ], { duration: 1000, iterations: 1, fill: 'forwards' }); x -= 60; } function myMoveDown(){ anim=item.animate([ // keyframes { transform: `translate(${x}px, ${y}px)` }, { transform: `translate(${x}px, ${y+60}px)` } ], { duration: 1000, iterations: 1, fill: 'forwards' }); y += 60; // anim.onfinish(()=>{console.log("onfinish ran")}) } item.addEventListener('animationend', () => { console.log('Animation ended'); }); button{ display:inline-block; height:40px; width:80px; } #myContainer { width: 400px; height: 400px; position: relative; background: lightgray ; } #item { background: orange; position: absolute; right:30px; top:30px; height: 30px; width: 30px; border-radius:50%; } <p> <button onclick="myMoveLeft()">Left</button> <button onclick="myMoveDown()">Down</button> </p> <div id ="myContainer"> <div id="item"></div> </div>
Load an image (background-image CSS) dynamically using javascript
i'm trying to convert the background-image (css) into an object to change it's value but I couldn't find a solution. At the moment there are 2 images in the script the first one is a circle loader script and the second one is a script that based of the value of the range it shows or hides part of the picture. What i'm trying to do is to make load the picture inside the circle loader script. For instance if the circle loader is 30/100 it should show only display 30% of the picture inside the circle vertically. Here's the code: Live version: var bar = new ProgressBar.Circle(container, { color: '#aaa', // This has to be the same size as the maximum width to // prevent clipping strokeWidth: 4, trailWidth: 1, easing: 'easeInOut', duration: 1400, text: { autoStyleContainer: false }, from: { color: '#aaa', width: 1 }, to: { color: '#333', width: 4 }, // Set default step function for all animate calls step: function(state, circle) { circle.path.setAttribute('stroke', state.color); circle.path.setAttribute('stroke-width', state.width); var value = Math.round(circle.value() * 100); if (value === 0) { circle.setText(''); } else { circle.setText(value); } } }); bar.text.style.fontFamily = '"Raleway", Helvetica, sans-serif'; bar.text.style.fontSize = '2rem'; bar.animate(1.0); // Number from 0.0 to 1.0 var sldH = document.getElementById('slider-h'); var sldV = document.getElementById('slider-v'); var img = document.getElementById("image"); // attach change handlers to the sliders sldH.addEventListener('change', changeHandler); sldV.addEventListener('change', changeHandler); function changeHandler(e) { var isHorizontal = e.srcElement.id == 'slider-h'; // get the sliders values var valH = sldH.value; var valV = sldV.value; // calculate the percentage to pass an absolute length value // to the clip property and determine the static value var leftVal = calcPerc(img.width, valH); var topVal = -1 * calcPerc(img.height, valV); var clipVal = getClipVal(topVal, leftVal); // set the images' right offset clip accordingly img.style.clip = clipVal; } function calcPerc(range, val) { return range / 100 * val; } function getClipVal(top, left) { return 'rect(' + top + 'px, auto, auto, ' + left + 'px)'; } #container { margin: 20px; top: 80px; width: 200px; height: 200px; position: relative; background-image: url("http://pngimg.com/uploads/bitcoin/bitcoin_PNG47.png"); background-repeat: no-repeat; background-size: 100% 100%; } .btc{ background-image: url("http://pngimg.com/uploads/bitcoin/bitcoin_PNG47.png"); background-repeat: no-repeat; background-size: 100% 100%; } #slider-h, #slider-v, #image, #underlay { /* absolute positioning is mandatory for clipped elements (#image) */ position: absolute; } #slider-h, #image, #underlay { width: 192px; left: 100px; } #slider-h { top: 50px; left: 350px; } #slider-v { top: 192px; left: 200px;; width: 207px; -moz-transform: rotate(270deg); -webkit-transform: rotate(270deg); -o-transform: rotate(270deg); -ms-transform: rotate(270deg); transform: rotate(270deg); } #image, #underlay { top: 100px; left: 350px; height: 207px; } #image { /* initial clip state */ } #underlay { /*background-color: #4C76A5;*/ } <script src="https://rawgit.com/kimmobrunfeldt/progressbar.js/1.0.0/dist/progressbar.js"></script> <link href="https://fonts.googleapis.com/css?family=Raleway:400,300,600,800,900" rel="stylesheet" type="text/css"> <div id="container" > </div> <input type="range" min="0" max="100" id="slider-h" value="0" /> <input type="range" min="-100" max="0" id="slider-v" value="0" /> <div id="underlay"></div> <img src="http://pngimg.com/uploads/bitcoin/bitcoin_PNG47.png" id="image" /> I hope somebody can help! Thanks
How to animate cloned element?
What i want to do is looped horizontaly text slider. My attempt - when end of text is displayed, clone this element move to right end and play animation for cloned element again - but i can't achieve that because for some reason cloned element don't want to animate. This is chrome only app Here is jsfiddle. CSS #elm { width: 100px; height:20px; overflow: auto; overflow-y: hidden; position:absolute; top:10px; left:10px; } #elm::-webkit-scrollbar { width: 0 !important; height: 0 !important; } #elm .inner-elm { position:absolute; white-space: nowrap; } HTML <div id="elm"> <div class="inner-elm"> 11111111 2222222 333333 4444444</div> </div> JS var elm_right = $('#elm').offset().left + $('#elm').outerWidth(); var settings = { duration: 5000, easing: 'linear', step: function() { var this_right = $(this).offset().left + $(this).outerWidth(); // make some clone if(!$(this).hasClass('cloned') && ((this_right + 50) < elm_right)) { $(this).addClass('cloned'); var clone = $(this).clone(true); clone.addClass('cloned') .css('left', $('#elm').width()) .appendTo('#elm') .animate({right:100}, settings); } // remove parent if( (this_right - 20) < $('#elm').offset().left) { $(this).remove(); } } }; var that = $('#elm .inner-elm'); that.animate({right: that.outerWidth()}, settings);
You could use CSS3 transitions for a simple animation like this. fiddle var interval = 2000; var nextTimeout = 200; var wrapper = $('#elm'); var elem = $('.inner-elm', wrapper); elem.addClass('move'); setInterval(function(){ var clone = elem.clone(true); elem.remove(); elem = clone; elem.removeClass('move'); wrapper.append(elem); setTimeout(function(){ elem.addClass('move'); }, nextTimeout); }, interval); #elm { height:20px; overflow: auto; overflow-y: hidden; position:absolute; top:10px; left:10px; } .inner-elm{ transition: all 2s linear; -ms-transform: translate(110%, 0); /* IE 9 */ -webkit-transform: translate(110%x, 0); /* Safari */ transform: translate(110%, 0); } .move{ -ms-transform: translate(-130%, 0); /* IE 9 */ -webkit-transform: translate(-130%, 0); /* Safari */ transform: translate(-130%, 0); } #elm::-webkit-scrollbar { width: 0 !important; height: 0 !important; } #elm .inner-elm { position:relative; white-space: nowrap; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <div id="elm"> <div class="inner-elm">Stackoverflow</div> </div>
appendTo is not working with each in jquery
I have to apply animation on each "div". But it is applying just in first one not in all. According to me "$("#viewport").each(appendTo(c));" this line is not working in javascript code. HTML code: <div id="viewport"></div> <div id="viewport"></div> <div id="viewport"></div> Javascript code: $(window).load(function(){ $(function () { var a = 0; for (; a < 15; a += 1) { setTimeout(function b() { var a = Math.random() * 1e3 + 5e3, c = $("<div />", { "class": "smoke", css: { opacity: 0, left: Math.random() * 200 + 80 } }); **$("#viewport").each(appendTo(c));** $.when($(c).animate({ opacity: 1 }, { duration: a / 4, easing: "linear", queue: false, complete: function () { $(c).animate({ opacity: 0 }, { duration: a / 3, easing: "linear", queue: false }) } }), $(c).animate({ bottom: $("#viewport").height() }, { duration: a, easing: "linear", queue: false })).then(function () { $(c).remove(); b() }); }, Math.random() * 3e3) } }()); });
Use more than one element with the same id is absolutely bad practice! Put "vieport" as class and use $( ".viewport" ) The .each() function take a function as first argument and in that function context, the "this" variable will contain the element $( ".viewport" ).each( function() { $( this ).appendTo( c ); });
If I am not wrong. You want something like this: HTML: <div id="viewport"> <div class="smoke smoke1"></div> <div class="smoke smoke2"></div> <div class="smoke smoke3"></div> </div> JS: (function () { "use strict"; $('#viewport .smoke').each(function ns () { var initialTop = $(this).position().top; $(this).animate({ top: - $(this).height() }, Math.random() * 2000 + 2000, function () { $(this).css({ top: initialTop, opacity: 0 }); }).animate({ opacity: 1 }, ns) }); }()); CSS: #viewport { position: relative; width: 400px; height: 300px; margin: 100px auto; border: 1px solid #333333; overflow: hidden; } #viewport .smoke { position: absolute; width: 20px; height: 40px; background: rgba(0, 0, 0, 0.5); } #viewport .smoke1 { left: 140px; top: 260px; } #viewport .smoke2 { left: 180px; top: 260px; } #viewport .smoke3 { left: 220px; top: 260px; } Please see demo here: http://jsfiddle.net/1rf31eyL/ You may check another example demo here: http://gettycreations.com/
first of all add function in $.each like "$("#viewport").each( function(){ $(this).appendTo(c); } ); and then try this, $("[id=viewport]") When you use $("#viewport") it selects only the first element with the given ID. However, when you select by attribute (e.g. id in your case), it returns all matching elements, like so: $("[id=viewport]")
Audio and animation in sync in javasctipt loop
I'm using my limited javascript skills to try an piece togheter a script that can play a small animation and a sound clip in a loop. This is what i've got so far: <script> function PulseArea() { $('.area-pulse').stop(); $('.area-pulse').css({ width: 38, height: 38, top: 217, left: 77, opacity: 1 }); $('.area-pulse').animate({ width: 700, height: 700, top: -114, left: -254, opacity:0 }, 4000, null, function () { setTimeout("PulseArea()", 400); }); } $(function() { var sample = new Audio("audio/water-droplet-1.wav"); function playSample() { sample.pause(); sample.currentTime = 0; sample.play(); } }) $(document).ready(function() { for (i=0;i<=5;i++) { PulseArea(); playSample(); } }) </script> Related css: .area-pulse { display:block; width:38px; height:38px; position:absolute; top:217px; left:77px; background-color:transparent; zoom: 1; } .area-pulse img { opacity:1; width:100%; height:100%; display:inline-block; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../img/area_pulse.png', sizingMethod='scale'); background-color:transparent; } Related HTML: <div style=" width:954px; height:600px; position:absolute;" class="area-pulse"><img src="img/area_pulse.png" /></div> It's the sound that is the problem. The animation plays fine. Any suggestions? I've searched StackOverflow, but can't find anything like this. Thanks for taking an interest!
Your function playSample is defined inside of anonymous function scope, so it can't be called from the document.ready scope. If you move all of it into the same scope, it will work fine: $(document.ready(function () { function PulseArea() { $('.area-pulse').stop(); $('.area-pulse').css({ width: 38, height: 38, top: 217, left: 77, opacity: 1 }); $('.area-pulse').animate({ width: 700, height: 700, top: -114, left: -254, opacity:0 }, 4000, null, function () { setTimeout("PulseArea()", 400); }); } var sample = new Audio("audio/water-droplet-1.wav"); function playSample() { sample.pause(); sample.currentTime = 0; sample.play(); } for (i=0;i<=5;i++) { PulseArea(); playSample(); } });
I don't know what was wrong with the script in my question, but i solved it by rewriting it. I hope someone else can be helped by this. :) <script> function PulseArea() { var audioElement = document.createElement('audio'); audioElement.setAttribute('src', 'audio/waterdrop.mp3'); audioElement.play(); $('.area-pulse').stop(); $('.area-pulse').css({ width: 16, height: 16, top: 227, left: 89, opacity: 1 }); $('.area-pulse').animate({ width: 700, height: 700, top: -114, left: -254, opacity:0 }, 4000, null, function () { setTimeout("PulseArea()", 400); }); } $(document).ready(function() { for (i=0;i<=5;i++) { PulseArea(); } }) </script>