My site is using Google web design code with custom functions.
I need to delete multiple image using the mouse. When the user goes out of the image the first image needs to be delated. When the user enters the document again, he is able to delete the following image.
My problem is that when the user goes out of the document all the images are going to be delayed.
I need to select different image by CSS ID --> WHR. Something's not working. I'm looking for a different solution.
<div class="gwd-page-content gwd-page-size">
<gwd-image id="gwd-image_3" source="assets/SO-91650-Solferino_Tamaro_01.03-30.03_300X250.jpg" scaling="stretch" class="gwd-image-1s8p"></gwd-image>
<gwd-image id="gwd-image_2" source="assets/SO-91650-Solferino_Panebianco_01.03-
<gwd-image id=" gwd-image_1 " source="assets/SO-91650-Solferino_Sinclair_05.03-05.04_300X250.jpg " scaling="stretch " class="gwd-image-on7n "></gwd-image>
<gwd-taparea id="gwd-taparea_image1 " class="gwd-taparea-odyq " data-gwd-name="tap_image1 "></gwd-taparea>
<gwd-taparea id="gwd-taparea_1 " class="gwd-taparea-f0yc " data-gwd-name="first_interaction " data-gwd-tl-hidden=" "></gwd-taparea>
</div>
<script type = "text/javascript" gwd-events="registration">
// Support code for event handling in Google Web Designer
// This script block is auto-generated. Please do not edit!
gwd.actions.events.registerEventHandlers = function(event) {
gwd.actions.events.addHandler('gwd-taparea_image1', 'mousemove', gwd.image2, false);
};
gwd.actions.events.deregisterEventHandlers = function(event) {
gwd.actions.events.removeHandler('gwd-taparea_1', 'mousemove', gwd.auto_Gwd_taparea_1Touchenter, false);
gwd.actions.events.removeHandler('gwd-taparea_image1', 'mousemove', gwd.image2, false);
};
document.addEventListener("DOMContentLoaded", gwd.actions.events.registerEventHandlers);
document.addEventListener("unload", gwd.actions.events.deregisterEventHandlers);
</script>
//var MAX = 3;
var i = 1;
gwd.image2 = function(e) {
var i = 1;
var whr = "#gwd-image_" + i;
var whr2 = "#gwd-image_" + (i + 1);
$(document).mousemove(function(e) {
console.log(whr);
$(whr).css({
left: e.pageX - 150,
top: e.pageY - 125,
}, 10);
$(whr).css("-webkit-transform", "rotate(" + (e.pageX - 150) / 10 + "deg)");
$(document).mouseout(function(e) {
$(whr).animate({
left: (e.pageX - 150) * 3 + 'px'
}, "fast", function() {
gwd.actions.events.setInlineStyle('gwd-image_1', 'display: none;');
i = i + 1;
})
})
})
}
Related
In my code, I wanted to create a mouse trail with 1)random images 2)a blurring effect 3) opacity transition 4) a max amount of divs in the trail.
Similar to this: https://tympanus.net/Development/ImageTrailEffects/
Point 2 and 3 are done, however I am stuck on point 4. The trail has a crazy strobe effect right now, which I dislike. I want it to be less sensitive and more subtle. Some kind of limitation on them. I've added a counter where I can count the amounts of divs created, but I'm unsure and stuck as to what to do now. I've looked at setInterval, but when I apply it to my function it's not working
I've also had some issues with the creation of an array for the random backgrounds, but I'm sure I'll figure that out. The question is mostly about how to limit and control the creation of the trail, but if anyone has a tip/link as to how to create the random background images, I am all ears.
Here the code I have up till now
window.onload= function() {
window.addEventListener('mousemove', function(e) {
var animate= function(i) {
var image= document.createElement('div'); //create a div named bubble
image.classList.add('trail');
var size= 60;
var sizepx= size+'px';
image.style.transition='2s ease';
image.style.position= 'fixed';
image.style.top= e.pageY - size/2 + 'px';
image.style.left= e.pageX - size/2 + 'px';
image.style.width= sizepx;
image.style.height= sizepx;
image.style.background= 'hsla(' +
Math.round(Math.random()*360) + ', ' +
'100%, ' +
'50%';
// image.style.background= 'black';
image.style.border= 'white 1px solid';
image.style.pointerEvents= 'none';
image.style.zIndex= 1;
document.body.appendChild(image);
//opacity and blur animations
window.setTimeout(function() {
image.style.opacity = 0;
image.style.filter = 'blur(6px)';
}, 80);
window.setTimeout(function() {
document.body.removeChild(image);
}, 2100);
};
var numItems = document.querySelectorAll('.trail').length;
console.log(numItems);
for (var i= 1; i <= 1; i+= 1) {
animate(i);
}
});
};
A fiddle:
https://jsfiddle.net/opufnsvz/
Here you go mate ( I used Unsplash for the random image source ), loading images on the fly gives unwanted effects, so they have to be preloaded. you can change the timesPerSecondto control the frequency
const numberOfImages = 10;
const imageSources = Array.from(Array(numberOfImages).keys()).map((i) => 'https://source.unsplash.com/collection/9948714?' + i);
// how many times to fire the event per second
const timesPerSecond = .1;
function preloadImages(images) {
for (i = 0; i < images.length; i++) {
let l = document.createElement('link')
l.rel = 'preload'
l.as = 'image'
l.href = images[i]
document.head.appendChild(l);
}
}
function animate(e) {
var image= document.createElement('div'); //create a div named bubble
image.classList.add('trail');
var size= 60;
var sizepx= size+'px';
image.style.transition='2s ease';
image.style.position= 'fixed';
image.style.top= e.pageY - size/2 + 'px';
image.style.left= e.pageX - size/2 + 'px';
image.style.width= sizepx;
image.style.height= sizepx;
image.style.backgroundImage = 'url(https://source.unsplash.com/collection/9948714?'+ Math.floor(Math.random()*numberOfImages) +')';
image.style.backgroundSize = 'cover';
image.style.border= 'white 1px solid';
image.style.pointerEvents= 'none';
image.style.zIndex= 1;
document.body.appendChild(image);
//opacity and blur animations
window.setTimeout(function() {
image.style.opacity = 0;
image.style.filter = 'blur(6px)';
}, 80);
window.setTimeout(function() {
document.body.removeChild(image);
}, 2100);
};
window.onload= function() {
preloadImages(imageSources);
var wait = false;
window.addEventListener('mousemove', function(e) {
if (!wait) {
wait = true;
setTimeout(() => { wait = false }, timesPerSecond * 1000);
animate(e);
}
});
};
I am relatively new to coding. I downloaded a html template that is a timeline that I want to use for a project I am busy with. I am not using this as a website or to sell it or anything it is merely educational. I know how to edit the html and change it to my own words and add images etc. My problem is, I think, with the javascript. So what happens is,when you scroll on the timeline and it gets to an image the background changes to the active image and so it changes to every different image on the timestamps.
When i add new html code to essentially add more timestamps to the timeline the new timestamps image does not appear as the background. It seems as if it has a set length. I am completely clueless when it comes to javascript and would really appreciate it if someone could assist me with this. Here is the js file:
(function($) {
$.fn.timeline = function() {
var selectors = {
id: $(this),
item: $(this).find(".timeline-item"),
activeClass: "timeline-item--active",
img: ".timeline__img"
};
selectors.item.eq(0).addClass(selectors.activeClass);
selectors.id.css("background-image", "url(" + selectors.item.first().find(selectors.img).attr("src") + ")");
var itemLength = selectors.item.length;
$(window).scroll(function() {
var max, min;
var pos = $(this).scrollTop();
selectors.item.each(function(i) {
min = $(this).offset().top;
max = ($(this).height() + $(this).offset().top);
var that = $(this)
if (i == itemLength - 2 && pos > min + $(this).height() / 2) {
selectors.item.removeClass(selectors.activeClass);
selectors.id.css("background-image", "url(" + selectors.item.last().find(selectors.img).attr('src') + ")");
selectors.item.last().addClass(selectors.activeClass)
} else if (pos <= max - 40 && pos >= min) {
selectors.id.css("background-image", "url(" + $(this).find(selectors.img).attr('src') + ")");
selectors.item.removeClass(selectors.activeClass);
$(this).addClass(selectors.activeClass);
}
});
});
}
})(jQuery);
$("#timeline-1").timeline();
The HTML:
<div class="timeline-item" data-text="Text">
<div class="timeline__content"><img class="timeline__img" src="BG1.jpg"/>
<h2 class="timeline__content-title">
7 October 9:53 PM</h2>
<p class="timeline__content-desc">Text</p>
</div>
</div>
I've been googling for hours and trying out different methods and can't get this to work.
I'm trying to do is: if there is no box, then add a box. If there is a box where you're clicking, then remove the box.
So far, it removes a box but automatically adds a new one, leaving a box on the screen when there should be none.
How do I get the box removed when someone clicks on it -- in pure Javascript.
Here is my jsfiddle so you can see what's happening. https://jsfiddle.net/on6z83ko/10/
Code:
addEventListener('click', createBox);
function createBox(event){
var box = document.createElement('div');
document.body.appendChild(box);
box.className = "box";
box.style.visibility="visible";
box.style.left = event.pageX + 'px';
box.style.top = event.pageY + 'px';
var mouse = event.currentTarget;
mouse.click = (mouse.click || 0) +1;
console.log(mouse.click);
box.addEventListener("click", function(){
var deleteBox = document.getElementsByClassName('box');
if(deleteBox){
box.remove();
}
});
}
document.addEventListener('click', createBox);
function createBox(event){
var target = event.target;
if(target.className === 'box'){
target.parentNode.removeChild(target);
} else {
var box = document.createElement('div');
box.className = "box";
box.style.visibility="visible";
box.style.left = event.pageX + 'px';
box.style.top = event.pageY + 'px';
document.body.appendChild(box);
}
}
From how I read the question, this will not add a box when there is one on the screen, and remove it when it
is clicked, then add another on the next click.
https://jsfiddle.net/on6z83ko/40/
addEventListener('click', createBox);
var box;
var boxBounds = {
Left: 0,
Right: 0,
Top: 0,
Bottom: 0
};
function createBox(event) {
if (box == null) {
box = document.createElement('div');
var target = event.target;
document.body.appendChild(box);
box.className = "box";
box.style.visibility = "visible";
box.style.left = event.pageX + 'px';
box.style.top = event.pageY + 'px';
return;
}
//alert(event.pageX + "," + event.pageY);
boxBounds.Left = parseInt(box.style.left, 10);
boxBounds.Right = boxBounds.Left + 100;
boxBounds.Top = parseInt(box.style.top, 10);
boxBounds.Bottom = boxBounds.Top + 100;
//alert("");
//alert("L" + boxBounds.Left + " R" + boxBounds.Right + " T" + boxBounds.Top + " B" + boxBounds.Bottom);
//remove if in bounds of box
if ((event.pageX >= boxBounds.Left) && (event.pageX <= boxBounds.Right) &&
(event.pageY >= boxBounds.Top) && (event.pageY <= boxBounds.Bottom)) {
//alert("InBOX");
document.body.removeChild(box);
box = null;
return;
}
}
Is this what you're looking for?
JSFiddle Example
This was the key-point:
if(target.classList.contains('box')){
target.remove();
}
You can create an even handler on your box and stop propagation to prevent main click event, and remove the box in that event handler. I cant edit your code from my phone :(
I really need help.
Here's what I'm trying to do: I wanna create 3 div elements which are flying from top to bottom of div (#lang_flying_objects) and when i click "the right one" (by Id), I need to create three more. When I create them I assign them all class "snowflakes".
But I am only able to click on the first group and my onclick function recognize the right id, but when other group is created it won't trigger the onclick function.
PLEASE HELP ME.
Here is the code I'm using:
<script>
function fallingStuff() {
var $snowflakes = $(),
createFallingStuff = function () {
var $snowflake = $('<div class="snowflakes" id="first"></div>');
$snowflake.css({
'left': (Math.random() * ($('#lang_flying_objects').width()/3 - offset)) + 'px',
'top': (-(20 + offset)) + 'px'
});
// add this snowflake to the set of snowflakes
$snowflakes = $snowflakes.add($snowflake);
var $snowflake2 = $('<div class="snowflakes" id="second" ></div>');
$snowflake2.css({
'left': (Math.random() * ($('#lang_flying_objects').width()/3 - offset) + $('#lang_flying_objects').width()/3) + 'px',
'top': (-(20 + offset)) + 'px'
});
// add this snowflake to the set of snowflakes
$snowflakes = $snowflakes.add($snowflake2);
var $snowflake3 = $('<div class="snowflakes" id="third"></div>');
$snowflake3.css({
'left': (Math.random() * ($('#lang_flying_objects').width()/3 - offset) + 2*$('#lang_flying_objects').width()/3) + 'px',
'top': (-(20 + offset)) + 'px'
});
// add this snowflake to the set of snowflakes
$snowflakes = $snowflakes.add($snowflake3);
$('#falling_zone').prepend($snowflakes);
},
runFalling = function() {
$snowflakes.each(function() {
var singleAnimation = function($flake) {
$flake.animate({
top: "500px",
opacity : "0"
}, 10000);
};
singleAnimation($(this));
});
};
createFallingStuff();
runFalling();
}
fallingStuff();
</script>
And here is my onclick function:
<script>
$('.snowflakes').on('click', function() {
var idInputed = $(this).attr('id');
if(idInputed == "first") //for example
{
fallingStuff();
}
});
</script>
Why it won't recognize the onclick function for second group of created divs?
Since the elements are created dynamically, you should use event delegation:
$(document).on('click', '.snowflakes', function() {
var idInputed = $(this).attr('id');
if(idInputed == "first") {
fallingStuff();
}
});
I have a javascript function that resize and centers images based on the surrounding container size (that in turn depend on the window size). I use jquery as js framework. The function need to be executed after document load (on document ready) but also when and if the user changes size of the browser, ie I have the following running in the html-document:
$(document).ready(function(){
fixImageSizes();
});
$(window).resize(function() {
fixImageSizes();
});
But for some unknown reason the function is only executed when a user resizes the browser and not after loading. Can anyone please help with this?
(my function is below for knowledge...)
function fixImageSizes()
{
var cw = $('#imagecontainer').width();
var ch = $('#imagecontainer').height();
$('#imagecontainer img').each(function()
{
var iw = $(this).css('width');
var ih = $(this).css('height');
if (parseInt(iw) < parseInt(cw)) // image width < viewport
{
var newih = Math.ceil(parseInt(ih) * parseInt(cw) / parseInt(iw)) + 'px';
var newimargint = '-' + Math.ceil(parseInt(newih)/2) + 'px';
var newimarginl = '-' + Math.ceil(parseInt(cw)/2) + 'px';
$(this).css({'width':cw,'height':newih,'margin-left':newimarginl,'margin-top':newimargint});
}
if (parseInt(ih) < parseInt(ch)) // image height < viewport
{
var newiw = Math.ceil(parseInt(iw) * parseInt(ch) / parseInt(ih)) + 'px';
var newimargint = '-' + Math.ceil(parseInt(ch)/2) + 'px';
var newimarginl = '-' + Math.ceil(parseInt(newiw)/2) + 'px';
$(this).css({'width':newiw,'height':ch,'margin-left':newimarginl,'margin-top':newimargint});
}
if (parseInt(ih) > parseInt(ch) && parseInt(iw) > parseInt(cw)) // viewport smaller than image, shrink image
{
if (parseInt(ch) - parseInt(ih) > parseInt(cw) - parseInt(iw)) // difference is less on height
{
var newiw = Math.ceil(parseInt(iw) * parseInt(ch) / parseInt(ih)) + 'px';
var newimargint = '-' + Math.ceil(parseInt(ch)/2) + 'px';
var newimarginl = '-' + Math.ceil(parseInt(newiw)/2) + 'px';
$(this).css({'width':newiw,'height':ch,'margin-left':newimarginl,'margin-top':newimargint});
}
else // difference less on width
{
var newih = Math.ceil(parseInt(ih) * parseInt(cw) / parseInt(iw)) + 'px';
var newimargint = '-' + Math.ceil(parseInt(newih)/2) + 'px';
var newimarginl = '-' + Math.ceil(parseInt(cw)/2) + 'px';
$(this).css({'width':cw,'height':newih,'margin-left':newimarginl,'margin-top':newimargint});
}
}
});
You should use the load event instead of the ready event.
The ready event runs after the document has loaded, but before the images has loaded, so you won't have the correct size of all elements.
The function is probably executing (you can double-check with a simple alert), but the images you are "fixing" is probably not loaded yet. You can use the window.onload event or listen to the image load event like this:
var scale = function() {
// rescale
};
$('#imagecontainer img').each(function() {
this.complete ? scale.call(this) : $(this).load(scale);
});
Try this:
$(window).load(function (){
fixImageSizes();
});