Changing Beginning of img [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How to change the "start" of image?
I mean that the img is in the immobility only it's beginning is moving
I want change it CSS or HTML, in the last resort in JavaScript
Something like this: FIRST PICTURE (This picture on click change it's beginnig).
... It transform to it: SECOND PICTURE
Any idea? :/

<div class="myImg"> </div>
<style>
.myImg{
background: url('myimg.jpg');
}
.myImg2{
background: url('myotherimg.jpg');
}
</style>
<script type="javascript/text">
$('.myImg').click(function() {
$(this).removeClass('myImg');
$(this).addClass('myImg2');
});
</script>
Ofcourse you'll have to use jquery link to get jQuery latest update.
So what have i actually done here?
I've used jQuery to check if user click div .myImg if he did i just switch bettwen the div's classes and each other class got different image.
Hope i helped.

I'm not entirely sure what you're asking, but it sounds like you might want to do CSS sprites.
http://css-tricks.com/css-sprites/
If you want to change the image on click, you'll need some code. I won't spend too much time on that, given your question is hard to decipher, but if it were me, I would create a class per sprite, then toggle between the two classes based on the click event.

Related

Move down the scroll when you click a photo (html) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
If I click on the image, it is supposed to move me to another part of the scroll. Problem is, the image is included in a header (like a banner) which initiated as a class. How could i implement js code inside an image that belongs to that class? Or should i create a class inside a class?
First of all you don't need to use javascript at all
You can place your img inside a tag with href="#id-of-element". In in some other place in your html you can place element with id="id-of-element". In that case when you click on img it will scroll you to this element (it will not be a smooth scroll but more like 'teleportation').
If you insist on smooth scroll then you can do it like that. This will cover all anchors like a href="#..." but if you want it to only work on this very image you can replace a[href^="#"] with classname/id of your image.
This example for some reason doesn't work here for some reason but work locally, so you have to check this yourself https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
document.querySelector('a[href^="#"]').addEventListener('click', (e) => {
e.preventDefault();
const id = e.target.getAttribute('href');
document.querySelector(id).scrollIntoView({behavior: "smooth", block: "center", inline: "nearest"});
});
<a href="#id-of-element">
this is image
</a>
<div style="height: 1000px"></div>
<div id="id-of-element">content</div>
You can try using document.querySelector(selector)
selector example - .header .banner img

How to achieve animation effects [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
In this website, the header is animated when you scroll the page a bit. Can you please advise what techniques, tools and best practices (broadly) one may use to achieve this and similar effects?
Use javascript to add a class to the header element when the scroll position is at the top (bind to the onscroll event and check if at the top or not).
Then define new postiions for your elements in css when that class is applied, and use the transition css property to animate it
Well for the animation occurring when you scroll you would use
$(document).scroll(function() {
if($(document).scrollTop() === 0);
} else {
});
As for the animation, there are many ways to do this and the question is too broad, as stated above.
the way I personally do it, is to add a class to the body on scroll. I then style the particular element I want to change (in your case the header) accordingly. I add it to the body instead of the particular element in case I want to manipulate other selectors, etc.
This is the JS I use:
jQuery(window).scroll(function() {
var scroll = jQuery(window).scrollTop();
if (scroll >= 50) { //distance scrolled before effect takes place
jQuery("body").addClass("scrolled");
} else {
jQuery("body").removeClass("scrolled");
}
});
Then I'd add some CSS for example:
header{height:100px;transition:all, .4s, ease; /*Make sure to use all cross-browser markup*/;}
body.scrolled header{height:50px;}

Does onclick work with any positioning? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I mage a cog symbol that is supposed to have an onclick function that triggers a function. When I click it does not work. Could it matter that it has absolute positioning? How can I fix this?
I used this HTML:
<div id="cogdiv" onclick="theme()">
<h1 id="cog" class="themechange fa fa-cog" onclick="theme()"></h1>
</div>
and this JS:
var theme = function() {
document.getElementById('themes').style.animation = "themesappear 2s forwards";
document.getElementById('themes').style.WebkitAnimation = "themesappear 2s forwards";
};
The function works when I run it in the console.
Inspect Element here for more code.
Absolute positioning has no effect on how the onClick event is being fired.
You are simply positioning your element below another element, so another element is blocking the mouse event
Try adding to your CSS:
#cogdiv {
z-index: 9999; //or greater, or 1. Just make sure it's bigger than the overlapping element's z-index
}
Reading up on what z-index and CSS stack-order might also help.

Two <div>'s stick together scroll [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm working on my diploma thesis, an interactive programming language which should run in browser eniroment using javascript.
In the moment I use an input line and enter event to start interaction and give feedback.
I would like to change this to have a textinput and a second field next to it where the result is shown. I use
<div id = "code" conetenteditable="true"> </div>
<div id= "result"><div>
Both are placed in a table row, so they are next to each other
how can I get result to scroll according to the actual scroll of code?
By the way I use jquery as library.
Thanks,
Rainer
In order for an element to be fixed on a page and remain there as you scroll, you must use CSS.
The code should look like:
<div id = "code" conetenteditable="true" style="position:fixed;"> </div>
<div id= "result" style="position:fixed"><div>
You can position them however you want with top, bottom, left, right.
Use css fixed position for your both dives.
like that:
#id,#result{
position: fixed;
}
I believe your problem is not with the element position but with the scroll of the text inside of the #result div.
If that's the case, you might want to use the overflow css property to have the div show an inner scrollbar.
#result {
overflow-y:auto;
max-height:100px;
}
in this example, the div will scroll if the text inside it stretches above 100px.

How to create an highlight animation when a control gets focused [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to create a hightlight html inputs when they get focused using css and javascript, and jump through inputs highlighting the corners.
What I mean, is, when a text input get focused a quarter frame (L) locate in each corner, and when another input get focused those (L)s moves through the form to locate in the new focused control
I wanted to publish an image but I haven´t enough reputation points
Could anyone lead me how to do it?
Thanks
I don't know if you have to use javascript for some reason, but you can handle focus highlighting with just plain CSS.
input[type="text"]:focus,
textarea:focus,
select:focus
{
border-color:#0cf;
/* etc... */
}
That will color/highlight the borders of the input
simple, clean
EDIT:
I imagine it can be done, but its more than my CSS skills cover.
My only CSS thoughts are some kind of Gradient but I don't think you can gradient an outline, just borders. And you have wont have full browser coverage.
You might be able to do something with :before and :after by dropping an image left and right.
Javascript wise there is a Malsup plugin that you can look at, but nothing that will match exactly that image.
I could do it myself!!!
First you have to draw the four corner´s highlights on HTML:
<img id="clt" src="img/clt.png" border="0" style="position:absolute;visibility:hidden"></span>
<img id="crt" src="img/crt.png" border="0" style="position:absolute;visibility:hidden"></span>
<img id="crb" src="img/crb.png" border="0" style="position:absolute;visibility:hidden"></span>
<img id="clb" src="img/clb.png" border="0" style="position:absolute;visibility:hidden"></span>
After, create a JavaScript function:
function getFocusFunct(){
$("input[type=text], input[type=password], textarea").focus(function(){
var ctl=$(this);
var offset=ctl.offset();
var clt=$("#clt");
var crt=$("#crt");
var crb=$("#crb");
var clb=$("#clb");
clt.css('visibility','visible');
crt.css('visibility','visible');
crb.css('visibility','visible');
clb.css('visibility','visible');
clt.animate({top:offset.top-3, left:offset.left-3}, 150);
crt.animate({top:offset.top-3, left:offset.left+ctl.outerWidth()-4}, 150);
crb.animate({top:offset.top+ctl.outerHeight()-4,left:offset.left+ctl.outerWidth()-4}, 150);
clb.animate({top:offset.top+ctl.outerHeight()-4,left:offset.left-3}, 150);
});
$("input[type=text], input[type=password], textarea").blur(function(){
clt.css('visibility','hidden');
crt.css('visibility','hidden');
crb.css('visibility','hidden');
clb.css('visibility','hidden');
});
}
It´s super awesome effect!!! Try it :)

Categories

Resources