Bootstrap Popover - Images appearing in wrong position until 2nd click - javascript

I have a number of buttons on my page that when clicked should display content inside a Bootstrap Popover. The content is loaded from my Database and can be either text or an image which can vary in size.I have implemented a solution which is working correctly in IE however in Chrome and Firefox my popover is opening half way down the page when you first click the button. On the second click of the same button the image will then appear in the correct position with no issue just like in IE. If the content of the popover is text then it is displayed correct first time in all browsers but the issue appears for images in Chrome and Firefox. I have researched this but I cannot find anything that has helped it to work across all browsers. Any help would be great.
HTML
<button type="button" class="btn btn-default btn-xs" data-toggle="popover" data-container="body" data-content="#item.Content">#item.Title</button>
JS
$(document).ready(function () {
$('[data-toggle="popover"]').popover({
html: "true",
placement: "top",
});
});
CSS
popover{
width:auto!important;
max-width: none;
}

I believe the issue (at least this was the case for me) was one of caching. The button is clicked, the popover loads its content, the <img /> tag is added to the DOM and begins a fetch, the popover renders, and the fetch completes and is blitted to the screen.
The best solution I've found is to preload/the image. There are two ways you can do this. If you know your image url ahead of time, you can add a preload link. Alternatively, you can place the popover content that includes the image tags in a <div class="d-none">...</div> (or class="hidden" if you're using bootstrap 3).
<html>
<head>
<!-- case where image urls are known ahead of time -->
<link rel="preload" as="image" href="my-image.png" />
</head>
<body>
<!--
case where image urls are NOT known ahead of time
NOTE: #item.Content should not include <script> tags or the code will be run twice
-->
<div class="d-none">
#item.Content
</div>
<button type="button" class="btn btn-default btn-xs" data-toggle="popover"
data-container="body" data-content="#item.Content">
#item.Title
</button>
<script>
$(document).ready(() =>
$('[data-toggle="popover"]').popover({
html: "true",
placement: "top",
});
});
</script>
</body>
</html>

For anyone who may come across the same issue as this I have the found adding the line - trigger: 'hover', to the script allowed images to be loaded in the correct position on each browser.

Related

Reloading and Unloading CSS on Modal Event Listeners

I want to be able to unload css when the modal closes and reload css when the modal opens again. The reason for this is because I noticed that if the user stays on the page after closing the modal- the css remains and they'd have to refresh for the page to appear as it did before the modal was opened.
Another solution would be to change my CSS but as its hundreds of lines I would rather find a way to do this using javascript manipulation. Below is the code I was trying to use. Anyone know where I am going wrong? The modal opens with css but after closing the modal the modal does not reload the css when its opened again.
$('.modal').on('shown.bs.modal', function() {
window.loadCSS(`pathname/css/mycssfile.css`);
});
$('.modal').on('hidden.bs.modal', function () {
$("link[href*='mycssfile']").prop('disabled', true);
$("link[href*='mycssfile']").remove();
});
You can make the style display or not. attache the CSS file to the <head> in the page you want it to open like this:
<link rel="stylesheet" href="css/modal.css">
don't forget to make the modal display: none is the CSS file.
then make "open" link/ button like this:
<i class="eye far fa-eye"></i>
in the modal don't forget to make the "close" link/button:
<span style="font-size: 40px;" onclick="document.getElementById('open1').style.display='none'" class="close" title="Close Modal">×</span>

Popover scrolls and some content is hidden when called within a .sticky-top element

So, using Bootstrap 4, I have a navigation bar that is set to .sticky-top. There's a button that calls a popover, which works great until the page is scrolled a little, then the top part of the popover content gets hidden.
This wouldn't be too bad if it only happened upon scrolling. The real issue is that if the popover is opened AFTER scrolling, the contents are still hidden.
Here's a Fiddle showing the problem:
https://jsfiddle.net/vfpb7r2d/16/
And the most relevant bit of code:
<div class="sticky-top bg-dark p-2">
<div class="btn btn-primary" data-toggle="popover" title="More information..." data-content="Isn't there a way to keep the whole popover visible??? The longer the popover content becomes, the more of the content disappears with scrolling. (There does seem to be a maximum cutoff point, but I haven't tried to measure what it is.)" />Button</div>
</div>
Is there any way to fix this?
Add data-boundary="viewport" to the popover element.
<div class="btn btn-primary" data-toggle="popover" data-boundary="viewport" title="More information..." data-content="Isn't there a way to keep the whole popover visible?">Button</div>
https://jsfiddle.net/vfpb7r2d/22/

Tooltip bootstrap cannot show in edge at runtime

I am trying to add a tooltip boostrap for a div tag at runtime. It work well on Chrome but in Edge it does not work.
<div id="#company.Id" class="fit-container pointer_cursor row" data-toggle="tooltip" title="#company.Comment" data-placement="bottom"><.div>
At runtime i will add a tooltip for div, but after i added, when i hover on div tag, the tooltip show nothing. Even though i call $('div').tooltip() in javascript.
Please help me.
Its working in all browsers. May be you forgot to add some assets.
$('div').tooltip();
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
<div id="" class="fit-container pointer_cursor row" data-toggle="tooltip" title="tooltip test" data-placement="bottom">test</div>

Bootstrap popovers: Use of callbacks in order to show/hide overlay -doesn't work

I have a web app where I use Bootstrap-popovers a number of times. These popovers contains HTML and are used to display small forms and such.
The idea is to "dim" the background with a black transparent overlay whenever a popover is shown in order to minimize distractions for the user.
I've gotten the basics down quite easily, but I really cannot get it to work as I want.
The desired functionality is: Whenever a button triggers a popover, the overlay should show in the background, covering the rest of the page.
When the popover is dismissed, either by it's save button, by it's cancel button or by a second click on the original trigger button, the overlay shall hide again.
This is my code:
HTML
<html>
<head>
// loading bootstrap.css
// loading main.css
// loading jquery.min.js
// loading bootstrap.min.js
// loading main.js
</head>
<body>
<div class="fadeMe" style="display:none"></div>
// ...lots of content
<div id="newReward" class="reward-markup edit-area">
<a href="#!" id="addRewardBtn" class="btn btn-primary trigger">
ADD REWARD
</a>
<div class="head hide">
<h4>NEW REWARD</h4>
</div>
<div class="content hide">
// ...small HTML-Form which works fine
<button class="btn btn-warning pull-left" onclick="$('.popover').popover('hide');">CANCEL</button>
<button class="btn btn-warning pull-right" onclick="addReward()">SAVE</button>
</div>
</div>
</body>
</html>
MAIN CSS
.fadeMe
{
opacity:0.7;
filter: alpha(opacity=20);
background-color:#000;
width:100%;
height:100%;
z-index:1;
top:0;
left:0;
position:fixed;
}
MAIN JS
$(function(){
$(".trigger").on('show.bs.popover', function(){
$("div.fadeMe").show();
});
$(".trigger").on('hidden.bs.popover', function(){
$("div.fadeMe").hide();
});
});
$('.reward-markup > .trigger').popover({
html: true,
title: function () {
return $(this).parent().find('.head').html();
},
content: function () {
return $(this).parent().find('.content').html();
},
container: 'body',
placement: 'bottom'
});
This is not working though. Triggering the popover brings out the overlay as it should, but dismissing the popover with the original triggerbutton leaves the overlay without hiding it, and thereafter wrecks the popover functionality alltogether (cancel button functions as it should). Strange thing is though, that if you supplement the jquery show/hide calls with simple alerts, the whole things works fine with trigger button showing alerts accordingly:
$(".trigger").on('show.bs.popover', function(){
alert('The popover is about to show.');
});
$(".trigger").on('hide.bs.popover', function(){
alert('The popover is now hidden.');
});
And even more strange: If you add both alerts and jquery-calls trigger button will show the first alert and then the overlay, but a second click on trigger button will not show second alert and overlay will not hide:
$(".trigger").on('show.bs.popover', function(){
alert('The popover is about to show.');
$("div.fadeMe").show();
});
$(".trigger").on('hide.bs.popover', function(){
alert('The popover is now hidden.');
$("div.fadeMe").hide();
});
Could someone please help me understand what's going on here? It drives me nuts!
i faced some kind of this before ... just for help if it works with you
in the cancel button instead of .('hide'); the popover ...
just try to click the original button again like below ...
<button class="btn btn-warning pull-left" onclick="$('.reward-markup > .trigger').click()">CANCEL</button>
I hope it helps
also check this fiddle .... if it helps

FancyBox returning "The requested content cannot be loaded. Please try again later." with link

I'm using Fancybox to display inline content (a div with an image linked to a new page). The div and image display fine in the modal, but when the image is clicked to go to the new page, I get "The requested content cannot be loaded. Please try again later." error.
The FancyBox javascript is as follows:
<script type="text/javascript">
$(document).ready(function() {
$(".fancybox").fancybox().hover(function() {
$(this).click();
});
$("#fancybox-outer").fancybox().mouseleave( function() {
$("#fancybox-overlay").click();
});
});
</script>
And applies to the following clip of HTML:
<li class="fancybox-outer">
<a id="inline" href="#hover-image_0" class="fancybox">
<img src="http://website.com/file/id:63" style="margin-top: 32px" />
</a>
<p>1G2A</p>
<div style="display: none;"><div id="hover-image_0"><img src="http://website.com/file/id:64/ext:.png" class="img" /></div></div>
</li>
<li class="fancybox-outer">
<a id="inline" href="#hover-image_1" class="fancybox">
<img src="http://website.com/file/id:60" style="margin-top: 32px" />
</a>
<p>17</p>
<div style="display: none;"><div id="hover-image_1"><img src="http://website.com/file/id:61/ext:.png" class="img" /></div></div>
</li>
Does anyone see what might be causing the issue or what I need to correct?
Thanks!
Update: 1/19/2012 - I performed the same test on my server as the first answer (http://estorkdelivery.com/example/example.html) and found that I got the same response back. So it seems it's something with my server.
I still need help with this issue. Anyone have any ideas?
Thanks!
Update: 1/28/2012 - I've been working to find a solution for this problem, but I've run out of time and have gone with a completely different solution. I'm keeping this problem open in case anyone else runs across the same error or ultimately finds a solution. Thanks!
Your approach has many issues:
First bear un mind that fancybox gets its content from the href attribute of any selector bound to it so the link
<a class="fancybox" href="{target}" ...
should be bound to fancybox via the following script in order to work
$('.fancybox').fancybox();
Pretty obvious but in your approach, the link inside the opened fancybox (which targets to yahoo for instance) is not bound to fancybox itself; it will try to fire Fancybox for sure again but with no indication of what the content should be this time, hence the error "The requested content cannot be loaded. Please try again later." in other words, the link (inside the inline content) <a href="http://yahoo.com" ... is not bound to fancybox.
The only way that links inside fancybox can work and load content dynamically (without being bound to fancybox) is when the current content is an external html document and fancybox type was set to iframe (not your case)
Second, you opened an image so fancybox set the type to image by default, but then you are pretending to load a different type of content on the same box (yahoo for instance), which would require to set type to iframe and other options like width and height.
Third, since you are using inline content, please be aware of an existing bug in v1.3.x and its workaround to avoid further issues, you can learn more here
Last, I am not just lecturing here, it's more like the explanation of what is going on with your issue .... but the good news is that you just need to add some more lines of code to make it work the way you want:
1: you need to create a fancybox script for each type of content you want to open the second time (additionally to your existing one), so in your example you want to click the image inside fancybox and that should open yahoo ... then create this script
$('.fancyframe').fancybox({
'type':'iframe',
'width': 600, //or whatever you want
'height': 300
});
2: within your hidden inline content set that class to the link, so this code:
<div style="display: none;">
<div id="hover-image_0">
<img src="1_b.jpg" class="img" />
</div>
</div>
now should look like
<div style="display: none;">
<div id="hover-image_0">
<a class="fancyframe" href="http://www.yahoo.com"><img src="1_b.jpg" class="img" /></a>
</div>
</div>
Do the same for each hidden inline content. If you want to open another type of content in fancybox, just create a script accordingly. The rest of your code is OK (doesn't bother me to fire fancybox on hover)
SIDE NOTES: sites like google, yahoo, jquery and some others won't open in fancybox. Also make sure you have the proper DOCTYPE for fancybox to work properly (your sample page doesn't have any). See Unable to load google in iframe in fancybox for explanation.
I think I've figured out what's causing this issue, at least for me.
The problem seems to occur when you've got a class on one of your elements that is the same as the popup class.
For instance:
<a class="popup" href="#">Open the popup</a>
And you have something in your popup with the same class name, for instance:
<div class="popup">some text</div>
You'll get the error. Try changing the div class to something like popup-window - that should fix your error
In your example page, it works for me if I don't click. When you click, what page are you supposed to be taken to? Do you want it to just go to the next image?
In my personal preference, I don't care for the activation on hover. It gets really confusing, especially when most people go to instinctually click on an image. But in your case it opens on hover, then they click instinctually, then you get the error.
Do you get this same error when you're just using the functionality as it normally applies?
If you want, try to give each image a gallery name instance like so: rel="gallery" and see what happens. You should get the next/prev arrows showing up and working like you would expect.
But honestly, I would get rid of the hover functionality altogether. Or at least get rid of the mouseout() That's probably the most bothersome part.
Just use this to call the fancybox actions:
$("a[rel=gallery]").fancybox();
That way you can get rid of all of those classes that are probably causing the issues.
I hope that helps.
I just ran into this issue as well.
As it turns out, the href url is EXTRA case sensitive. (if that is possible)
Regardless double, triple check your href urls to insure that you have the case exactly correct.
my solution was:
<script type="text/javascript">
jQuery(document).ready(function() {
$(".fancybox").click(function() {
$.fancybox({
'padding' : 0,
'autoScale' : false,
'transitionIn' : 'none',
'titleShow' : false,
'showCloseButton': true,
'titlePosition' : 'inside',
'transitionOut' : 'none',
'title' : '',
'width' : 640,
'height' : 385,
'href' : this.href,
'type' : 'iframe',
'helpers' : {
'overlay' : {'closeClick': false}
}
});
return false;
});
});
</script>
If some of the images are showing and others are not, even though the markup is identical, then check this:
Try to lowercase all the letters in the filename and in the html. Fancybox seems to be quite case sensitive.
Check if the image has all the permissions allowed. I noticed my pictures didn't work properly on mobile because on my computer, everybody's permission was set to "no permission". I changed this to "read only" and it worked like a charm!
I just re-created your test on my local machine and it works fine for me (I ran this from the demo folder of the FancyBox install) :
<html>
<head>
<title>jQuery Fancybox Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js" type="text/javascript"></script>
<!-- Add fancyBox main JS and CSS files -->
<script type="text/javascript" src="../source/jquery.fancybox.js"></script>
<link rel="stylesheet" type="text/css" href="../source/jquery.fancybox.css" media="screen" />
</head>
<body>
<script type="text/javascript">
$(document).ready(function () {
$(".fancybox").fancybox().hover(function () {
$(this).click();
});
$("#fancybox-outer").fancybox().mouseleave(function () {
$("#fancybox-overlay").click();
});
});
</script>
<li class="fancybox-outer"><a id="inline" href="#hover-image_0" class="fancybox">
<img src="1_s.jpg" style="margin-top: 32px" />
</a>
<p>
Go to Yahoo</p>
<div style="display: none;">
<div id="hover-image_0">
<a href="http://www.yahoo.com">
<img src="1_b.jpg" class="img" />
</a>
</div>
</div>
</li>
<li class="fancybox-outer"><a id="inline" href="#hover-image_1" class="fancybox">
<img src="2_s.jpg" style="margin-top: 32px" />
</a>
<p>
Go to Google</p>
<div style="display: none;">
<div id="hover-image_1">
<a href="http://www.google.com">
<img src="2_b.jpg" class="img" /></a></div>
</div>
</li>
</body>
</html>
I having the same problem with Fancybox. You cannot have spaces or special character in href and div id. Use an URL slug if you are using page titles.
I was checking out all the possibilities and ran into the EXTRA case sensitivity issue as #BrettBumeter mentioned.
In fact, it is "so much case sensitive", I had to revrite the URL (href) path to my images from *.jpg to *.JPG (or whatever your file type extension might be).
Altering this solved my issue.
Don't apply the fancybox class to your LI element, do it to your A element.
I, after taking the above advice, changed ".png" to ".PNG" in the href. NOTE that the file names are a lower case extension but it only works with upper case extension in the href (rest of filename same)
Hope this helps.
My problem was giving inappropriate path to image e.g;
<a data-fancybox="gallery" href="../dist/imgs/nature/n1.png">
<img src="../dist/imgs/nature/n1.png"></a>
While the images loaded well locally, It could not do online.
Correcting the path solved the problem.
<a data-fancybox="gallery" href="imgs/nature/n1.png">
<img src="imgs/nature/n1.png"></a>
If you are trying to make Fancybox work with dynamic content, you may check this.
I was using the following code:
<a class="video__overlay" data-fancybox="gallery" href="<?= $blog_url ?>"></a>
My issue was that I was generating $blog_url dynamically, looking at a specific text file line, so a \n was being added at the end of $blog_url, without me noticing this.
So, I used trim() and everything works as expected.

Categories