Show and hide imges on tap button in AMP - html

Is possible to show one image and hide multiple images on button tap i AMP?
For exaplme, I have a 4 buttons (button1, button2, button3 and button4) and have 4 images (img1, img2, img3, img4). And I would like to do something like that, hit button1 and show img1, hit button2 show img2 but hide img1 and etc.
In simple way hit button to show image and hide image witch is currently on screen.
I tried to do it, but im stuck on something like this. But it won't works correctly.
<button class="button1" on="tap:img1.toggleVisibility">BUTTON1</button>
<button class="button2" on="tap:img2.toggleVisibility">BUTTON2</button>
<button class="button3" on="tap:img3.toggleVisibility">BUTTON3</button>
<button class="button4" on="tap:img4.toggleVisibility">BUTTON4</button>
<div>
<div id="img1" style=" position: absolute" hidden >
<amp-img src="img1.jpg" width="600" height="600"> </amp-img>
</div>
<div id="img2" style=" position: absolute" hidden >
<amp-img src="img2.jpg" width="600" height="600"> </amp-img>
</div>
<div id="img3" style=" position: absolute" hidden >
<amp-img src="img3.jpg" width="600" height="600"> </amp-img>
</div>
<div id="img4" style=" position: absolute" hidden >
<amp-img src="img4.jpg" width="600" height="600"> </amp-img>
</div>
</div>
I've tired with on="tap:AMP.setState({ hide: true })" and on="tap:AMP.setState({ hide: false })" but it works only for two images.

For a long time the question was unanswered, I will try to help and I hope the question is still relevant.
If I understand correctly, you want to display only image related to button id clicked at once. For simplicity of the example, I will not use images directly. Instead, we use multi-colored div's. The solution is binding hidden property to exact Id. Feel free to play with below enhanced code and css, and images if you like:
.container{
display: flex;
margin-left: 100px;
}
.box{
width: 30px;
height: 30px;
margin: 25px;
padding: 5px;
color: azure;
font-weight: 900;
}
.img1{
background: red;
}
.img2{
background: green;
}
.img3{
background: blue;
}
.img4{
background: yellow;
}
<button class="button1" on="tap:AMP.setState({id:1})">BUTTON1</button>
<button class="button2" on="tap:AMP.setState({id:2})">BUTTON2</button>
<button class="button3" on="tap:AMP.setState({id:3})">BUTTON3</button>
<button class="button4" on="tap:AMP.setState({id:4})">BUTTON4</button>
<div class="container">
<div id="img 1" class="img1 box" hidden [hidden] = "id == 1 ? false : true" >
img1
</div>
<div id="img 2" class="img2 box" hidden [hidden] = "id == 2 ? false : true" >
img2
</div>
<div id="img 3" class="img3 box" hidden [hidden] = "id == 3 ? false : true">
img3
</div>
<div id="img 4" class="img4 box" hidden [hidden] = "id == 4 ? false : true">
img4
</div>
</div>
Warm Regards,
V.

Related

jQuery Change image depending on the link data-key in each parent

I have a section 2 blocks: one - with 3 images, second - with 3 links. Each image has it's own class (class=".img1") that is connected to a definite link with datakey=".img1".
When I hover over each link the definite image is being shown.
The section is a repeater block, that has a loop of images inside (I use ACF for this).
So when I have multiple sections on the page, the link hover from one section changes images in all other sections.
I was trying to use .each() to specify the parent section and then call .hover for links, but it doesn't work the way I need. I'm stuck in this and seems need to use another option.
JSfiddle with 1 section - https://jsfiddle.net/vernigoranataly/Lnwmjq3c/42/
JSfiddle with 2 sections - https://jsfiddle.net/vernigoranataly/kLtz5v4c/4/
JS:
$('.section_product-category ').each(function() {
$('.prodcat_btn .button-link').hover(
function() {
$($(this).data("key")).addClass('active');
$($('.prodcat_btn .button-link').not(this).data('key')).removeClass('active');
},
function() {
$($(this).data("key")).removeClass('active');
$($('.prodcat-img1')).addClass('active');
}
);
});
HTML:
<section class="section_product-category ">
<div class="prodcat_imgs">
<div class="prodcat_img prodcat-img1 active">
<img width="720" height="970" src="https://i.postimg.cc/k4pHm2DW/CTA-image.png" class="attachment-full size-full">
</div>
<div class="prodcat_img prodcat-img2">
<img width="345" height="480" src="https://i.postimg.cc/GhwC8fhG/visit-us-wine-glass.jpg" class="attachment-full size-full">
</div>
<div class="prodcat_img prodcat-img3">
<img width="1035" height="1440" src="https://i.postimg.cc/3NLm6GRH/social-image-three.jpg" class="attachment-full size-full">
</div>
</div>
<div class="prodcat_text">
<h2>Category #1 links</h2>
<div class="prodcat_btn btn">
<a class="button-link" data-key=".prodcat-img1" href="https://google.com">Link text here</a><br>
<a class="button-link" data-key=".prodcat-img2" href="https://google.ca">One more link btn</a><br>
<a class="button-link" data-key=".prodcat-img3" href="https://google.ua">Link text #3</a><br>
</div>
</div>
</section>
Update
I misunderstood what one part of your code was trying to do, and had replaced it with a different approach. I've updated my answer to use that part of your original code.
The problem is because each set has a <div> with the same class, like prodcat-img1, and the code which makes an image active:
$($(this).data("key")).addClass('active');
which evaluates to, eg:
$('.prodcat-img1').addClass('active');
matches all <div>s with that class, ie every one on the page.
The solution is to target only the ones in the current <section>, using something like:
$(this)
.closest('.section_product-category')
.find($(this).data("key"))
.addClass('active');
$(this) is the current element which triggered the hover/unhover event;
.closest() will traverse up the DOM tree until it finds the first match. In this case we look for the parent <section> which encloses this set of links and images;
.find() searches down the DOM tree from the current element for elements matching the selector. In this case we look for the (single!) element inside the <section> we found with a class matching your data-key;
Next, The same problem exists with this line:
$($('.prodcat_btn .button-link').not(this).data('key')).removeClass('active');
It will target every div on the page with the relevant class (eg .prodcat-img1), not just the one in the current section.
We can use the same fix though - start at the parent <section>, find the divs with active class, and remove that class. We just wrap the whole selector in the same code as above:
$(this)
.closest('.section_product-category')
.find($($('.prodcat_btn .button-link').not(this).data('key')))
.removeClass('active');
There is one other issue with this line - if you remove the class from the <div>s after you add it to the one we want, you're left with none of them with the class! :-) You need to remove the class from everything first, then add it to just the one we want. You already have that the right way around in the hover-out handler, just not in this hover handler.
Another issue is this code:
$('.section_product-category ').each(function() {
$('.prodcat_btn .button-link').hover( ...
Here you are iterating over all .section_product-category on the page, and adding handlers for $('.prodcat_btn .button-link'). But $('.prodcat_btn .button-link') matches every one of those elements on the page. So on the first iteration, you add a handler which matches every $('.prodcat_btn .button-link') on the page. The second iteration, you do it all again! The handlers just add up, they don't overwrite each other, and this means that every time you mouse over one of your links, your handler code runs 2x, or 3x if you have 3 sets, etc. You can confirm this by putting a console.log() inside your hover function - you'll see as many log lines written as you have <section>s, for a single mouse-over.
If you're lucky they won't interfere with each other, but depending on what they do they can, and you end up with weird behaviour. You can just remove the iteration - the single selector matches everything.
Here's a working snippet, starting from your 2-section JSFiddle, with those issues fixed:
$('.prodcat_btn .button-link').hover(
function() {
// $($(this).data("key")).addClass('active');
// $($('.prodcat_btn .button-link').not(this).data('key')).removeClass('active');
let $section = $(this).closest('.section_product-category');
// My original approach to remove active classes in this section
// $section.find('.prodcat_img').not($(this)).removeClass('active');
// Your original approach, updated to only target the current section
$section.find($($('.prodcat_btn .button-link').not($(this)).data('key'))).removeClass('active');
$section.find($(this).data("key")).addClass('active');
},
function() {
// $($(this).data("key")).removeClass('active');
// $($('.prodcat-img1')).addClass('active');
let $section = $(this).closest('.section_product-category');
$section.find($(this).data("key")).removeClass('active');
$section.find('.prodcat-img1').addClass('active');
}
);
.section_product-category {
display: flex;
width: 90%;
margin-bottom: 20px;
}
.section_product-category>div {
width: 70%;
}
.section_product-category>div:first-child {
width: 30%;
}
h2 {
margin-bottom: 45px;
}
.prodcat_img {
display: none;
overflow: hidden;
position: relative;
padding-top: 135%;
border: 1px solid blue;
}
.prodcat_text {
padding: 20px 20px 20px 40px;
}
.prodcat_img img {
position: absolute;
top: 0;
height: 100%;
width: 100%;
object-fit: cover;
}
.prodcat_img.active {
display: block;
}
.button-link {
margin-bottom: 7px;
display:block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section class="section_product-category ">
<div class="prodcat_imgs">
<div class="prodcat_img prodcat-img1 active">
<img width="720" height="970" src="https://i.postimg.cc/k4pHm2DW/CTA-image.png" class="attachment-full size-full">
</div>
<div class="prodcat_img prodcat-img2">
<img width="345" height="480" src="https://i.postimg.cc/GhwC8fhG/visit-us-wine-glass.jpg" class="attachment-full size-full">
</div>
<div class="prodcat_img prodcat-img3">
<img width="1035" height="1440" src="https://i.postimg.cc/3NLm6GRH/social-image-three.jpg" class="attachment-full size-full">
</div>
</div>
<div class="prodcat_text">
<h2>Category #1 links</h2>
<div class="prodcat_btn btn">
<a class="button-link" data-key=".prodcat-img1" href="https://google.com">Link text here</a><br>
<a class="button-link" data-key=".prodcat-img2" href="https://google.ca">One more link btn</a><br>
<a class="button-link" data-key=".prodcat-img3" href="https://google.ua">Link text #3</a><br>
</div>
</div>
</section>
<section class="section_product-category ">
<div class="prodcat_imgs">
<div class="prodcat_img prodcat-img1 active">
<img width="720" height="970" src="https://i.postimg.cc/k4pHm2DW/CTA-image.png" class="attachment-full size-full">
</div>
<div class="prodcat_img prodcat-img2">
<img width="345" height="480" src="https://i.postimg.cc/GhwC8fhG/visit-us-wine-glass.jpg" class="attachment-full size-full">
</div>
<div class="prodcat_img prodcat-img3">
<img width="1035" height="1440" src="https://i.postimg.cc/3NLm6GRH/social-image-three.jpg" class="attachment-full size-full">
</div>
</div>
<div class="prodcat_text">
<h2>Category #2 links</h2>
<div class="prodcat_btn btn">
<a class="button-link" data-key=".prodcat-img1" href="https://google.com">Link text here</a><br>
<a class="button-link" data-key=".prodcat-img2" href="https://google.ca">One more link btn</a><br>
<a class="button-link" data-key=".prodcat-img3" href="https://google.ua">Link text #3</a><br>
</div>
</div>
</section>

Transparent Image Button

I made a button that opens to make sort of like a drop-down page. I also made a button to retract the page and put an image in it, but I want to make the button not show up, only the image. Can anyone help?
HTML:
<button onclick="close1()"><img src="https://cdn0.iconfinder.com/data/icons/user-interface-material-4-1/26/368-512.png" alt="close" width="30"></button>
Here's the solution:
.transparent_button {
background-color: transparent;
border: 0px;
}
<button class="transparent_button" onclick="close1()"><img src="https://cdn0.iconfinder.com/data/icons/user-interface-material-4-1/26/368-512.png" alt="close" width="30"></button>
But for a better-written solution, you can try this (this won't force CSS uslessly):
<img onclick="close1()"src="https://cdn0.iconfinder.com/data/icons/user-interface-material-4-1/26/368-512.png" alt="close" width="30">
The solution: only set transparent border and background for it.
button {
background: transparent;
border: none;
}
<button class="transparent_button" onclick="close1()"><img src="https://cdn0.iconfinder.com/data/icons/user-interface-material-4-1/26/368-512.png" alt="close" width="30"></button>
If you don't need the button, but just the image to be clicked on you can leave the button out and use the following code:
<img onclick="close1() src="https://cdn0.iconfinder.com/data/icons/user-interface-material-4-1/26/368-512.png" alt="close" width="30" />
Maybe, if you want a border you can box it and style that:
HTML:
<div class="imageBox">
<img onclick="close1() src="https://cdn0.iconfinder.com/data/icons/user-interface-material-4-1/26/368-512.png" alt="close" width="30" />
</div>
CSS:
.imageBox{
border:1px solid #c1c1c1;
/* Other additional styles */
}

How do I make a specific tag appear on click and hide the other one present in a div?

I'm trying to do a simple component that just change the icon img on click, it's two simple images. The problem is that the close icon doesn't even show up and the icon to open doesn't hide when I click on it.
I've tried adding (click)="isVisible = !isVisible" and [ngClass]="{ 'isVisible': isVisible }"
None of these seems to work, here is my HTML:
<div class="toggle" (click)="isVisible = !isVisible">
<div class="icon">
<img src="https://image.flaticon.com/icons/png/512/64/64576.png" class="icon-open" alt>
<img src="https://image.flaticon.com/icons/png/512/24/24139.png" class="icon-close"
[ngClass]="{ 'isVisible': isVisible }" alt>
</div>
</div>
I expect .icon-close to show up and '.icon-open' to hide when I click on .toggle
Here you should use angular's directive *ngIf.
<div class="toggle" (click)="isVisible = !isVisible">
<div class="icon">
<img *ngIf="isVisible" src="https://image.flaticon.com/icons/png/512/64/64576.png" class="icon-open" alt>
<img *ngIf="!isVisible" src="https://image.flaticon.com/icons/png/512/24/24139.png" class="icon-close" alt>
</div>
</div>
I would use a toggle class to toggle between 2 icons. I use something like below:
$().ready(function(){
$(".closeButton").click(function () {
$(".searchContents").slideToggle('1000');
$(this).find('i').toggleClass('fas fa-times fas fa-sort-down')
});
});
As you are using hide and show functionality you need to create a two class for hide and show like.
.block {
display: block;
}
.none {
display: none;
}
and use it like
<div class="toggle" (click)="isVisible = !isVisible">
<div class="icon">
<img src="https://image.flaticon.com/icons/png/512/64/64576.png" height="30px" class="icon-open" [ngClass]="{ 'isVisible': !isVisible }" alt>
<img src="https://image.flaticon.com/icons/png/512/24/24139.png" height="30px" class="icon-close"
[ngClass]="{ 'block': isVisible,'none':!isVisible }" alt>
</div>
please check full demo here Demo

Is it a way to add text beside img of button?

Is there a way get through to add text after img tag and it can be update everytime?
I'm using jsp and not support html5.
I have try through some example from here:
https://www.w3schools.com/howto/howto_css_image_text.asp
<button type="button" class="button" id="updatetext" style="background-color:Transparent;background-repeat:no repeat;border:none;padding:25px">
<img src="https://www.w3schools.com/howto/img_snow_wide.jpg" height="85" width="85" align="left">
</button>
The Result:
The text inside the image can be update.
img {
margin-bottom: -20px;
}
#some_id{
color: white;
}
<button type="button" class="button" id="updatetext" style="background color:Transparent;background-repeat:no repeat;border:none;padding:25px">
<img src="https://image.shutterstock.com/z/stock-photo-chinese-snack-and-chinese-tea-on-old-brown-wooden-table-in-dim-light-still-life-imge-and-select-1177215178.jpg" height="85" width="85" align="left"><p id = "some_id">text</p>
</button>
then you can use jsp by referencing to id to update text
You can use :after or :before selector to append or prepend text after any element.
img:after{
content: 'text after image'
}
<button type="button" class="button" id="updatetext" style="background-color:Transparent;background-repeat:no repeat;border:none;padding:25px">
<img src="..\icon\press.png" height="85" width="85" align="left">
</button>
You need to link the html message to a variable using jsp annotations, in this case is: <%= Variable %>, which will be updated somehow, also put a little bit of style on the page.
Try this:
<div style="display:inline-block;vertical-align:top;">
<button id="button_test">
<img src="https://cdn2.iconfinder.com/data/icons/facebook-ui-colored/48/JD-22-64.png" alt="img"/>
<div style="display:block;"><%= Variable %></div>
</button>
</div>
</div>

Revealing and hiding specific div with iframe inside with button

I´m just trying to do website for family member. I´ve got base html with css done, but there is one issue. I need to implement to page changing iframe that is linked to two kinds of forms in php. Right now I´ve figured out code for change hrefs, but these iframes have different heights so I want to close both of them into divs and reveal first or second with buttons.
Here is the code so far:
<section id="predplatne" class="clearfix">
<div class="predplatne_in">
<h2>Text</h2>
<div class="vyber">
<button class="button">1</button>
<button class="button">2</button>
</div>
<div class="box">
<div class="in">
<iframe allowfullscreen frameborder="0" height="1700" id="frame" name="frame" src="http://www.php" width="480"></iframe>
</div>
</div>
</div>
</div>
You can use JS for that. From what I've understood, you want to show an <iframe> when the button is clicked?
First off, create your <iframe>s and set both of their CSS display values to none, like so:
<div class="iframes">
<iframe allowfullscreen style="display: none" frameborder="0" height="300" width="480" name="frame1" src="http://example.com"></iframe>
<iframe allowfullscreen style="display: none" frameborder="0" height="300" width="240" name="frame2" src="http://thedragonring.me"></iframe>
</div>
Then, add some <button>s with a little script in their onclick attributes that will toggle whether the display attribute of the corresponding <iframe> is set to none or block, like so:
<div class="buttons">
<button onclick="
const el = document.querySelector('.iframes [name=frame1]');
el.style.display = el.style.display === 'none' ? '' : 'none';
">
Frame 1 (example.com)
</button>
<button onclick="
const el = document.querySelector('.iframes [name=frame2]');
el.style.display = el.style.display === 'none' ? '' : 'none';
">
Frame 2 (thedragonring.me)
</button>
</div>
Alternatively, you could add a single button that will toggle between the 2 iframes:
<button onclick="
const frame1 = document.querySelector('.iframes [name=frame1]'),
frame2 = document.querySelector('.iframes [name=frame2]'),
prev = frame1.style.display;
frame1.style.display = prev === 'none' ? '' : 'none';
frame2.style.display = prev === 'none' ? 'none' : '';
">
Toggle
</button>
Basically, all these snippets of JS do is inverse the display attribute of the target <iframe>. You can see a demo # http://jsfiddle.net/TheDragonRing/4bzrvnyh/
You can achieve with some methods below
#if1, #if2{
display:none;
}
#btn1:active ~ div #if1, #btn2:active ~ div #if2{
display:block;
}
#rd1:checked ~ div #if1, #rd2:checked ~ div #if2{
display:block;
}
#cb1:checked ~ div #if1, #cb2:checked ~ div #if2{
display:block;
}
<!-- Reveal with active button -->
<button id="btn1">Reveal Frame 1</button>
<button id="btn2">Reveal Frame 2</button>
<br>
<!-- Reveal with checked radio -->
<input type="radio" id="rd1" name="reveal">
<label for="rd1">Reveal Frame 1</label>
<input type="radio" id="rd2" name="reveal">
<label for="rd2">Reveal Frame 2</label>
<br>
<!-- Reveal with checked checkbox -->
<input type="checkbox" id="cb1">
<label for="cb1">Reveal Frame 1</label>
<input type="checkbox" id="cb2">
<label for="cb2">Reveal Frame 2</label>
<div>
<iframe width="100px" id="if1" src="http://google.com">Test</iframe>
<iframe width="200px" id="if2" src="http://amazon.com">Test</iframe>
</div>
The easiest way is use javascript than just html and css
Hope it helps