I want my "Back to top" button to appear when the user scrolls about half down the webpage not immediately once the web page shows up. How would you be able to do that using HTML and CSS?
Element I want to appear about half way down and click to go back to the top:
<img id="upArrow" src="Images/arrow-up.png" alt="Up arrow">
Let me know if this works:
Edit: Added animation to return user to top on .click
<script>
$(window).scroll(function () {
if ($(window).scrollTop() > $('body').height() / 2) {
$('#upArrow').fadeTo(500, 1);
}
});
$('#upArrow').click(function(){
$("html, body").animate({ scrollTop: 0 }, "slow");
});
</script>
Set your upArrow as:
<img id="upArrow" style="opacity: 0;" src="Images/arrow-up.png" alt="Up arrow"/>
Alternative .js:
$(document).ready(function(){
$(window).on('scroll', function() {
var y_scroll_pos = window.pageYOffset;
var scroll_pos_test = 500 // Edit this number to define how far down the page the div fades in.
if(y_scroll_pos > scroll_pos_test) {
$('#upArrow').fadeTo(500, 1);
}
});
});
$('#upArrow').on("click",function(){
$("html, body").animate({ scrollTop: 0 }, "slow");
});
Related
I have a button that opens up a pop-up window for a contact form, but the pop-up is not scrolling on mobile, so I was just thinking that it might be easier to change the button for mobile only. Is there a way to plug in some code to make this work for now?
The code for the button is:
<a class="book-now call-popup" href="booking-form.html" title="">Book Now</a>
where if I take out "call-popup" it disables the popup and takes you to the url. Any way to set this up so that on mobile the "call-popup" is turned off/disabled?
EDIT:
Added the following
function checkWidth() {
if ($(window).width() < 758) {
$('#popup').removeClass('call-popup');
} else {
$('#popup').addClass('call-popup');
}
}
$(window).resize(checkWidth);
and
<a id="popup" class="book-now" href="booking-form.html" title="">Book Now</a>
But it is not working. I mean, when I inspect the page, it IS working correctly, but it is still calling the popup even when the class has been removed for some reason. And strangely enough, it is working on my fiddle, but not my site:
http://jsfiddle.net/5SM9u/51/
EDIT 2: Since it was asked, here is the popup js
/*=================== Book Now Popup ===================*/
$(".call-popup").on("click", function() {
$(".booking-popup").fadeIn();
return false;
});
$(".booking-popup").on("click", function() {
$(this).fadeOut();
return false;
})
$(".popup-inner, .datepicker").on("click", function(e) {
e.stopPropagation();
});
$(".booking-popup .close-btn").on("click", function() {
$(".booking-popup").fadeOut();
});
/*=================== Popup ===================*/
$(".call-popup-alert").on("click", function() {
$(".popup").fadeIn();
return false;
});
$(".popup").on("click", function() {
$(this).fadeOut();
return false;
})
$(".simple-popup-inner").on("click", function(e) {
e.stopPropagation();
});
$(".popup .close-btn").on("click", function() {
$(".popup").fadeOut();
});
Source page code:
About Us
Destination page code:
<h3><div id="aboutUs" ">About Us</div></h3>
when About Us link is clicked it points to url /rss-footer-documents.html#/termsOfRegistration.
I want it to point to /rss-footer-documents.html#termsOfRegistration.
Since it adds / after # automatically, it simply navigates to the page not to specific section.
Please tell me how to remove / in the url to appear that get added automatically.
Have you tried to simply add slash on your anchor link like this?
About Us
demo - http://jsfiddle.net/n1xtsx42/17/
$(document).ready(function () {
$('#nav a[href^="#"]').on('click', function (e) {
e.preventDefault(); /* adding this will remove # which is taken on click of a */
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, 1000, function () {
window.location.hash = target;
});
});
});
Scroll if window url contains #
if(window.location.hash) {
// if window location contains hash
var hash = window.location.hash.substr(1);
// getting value after hash
$target = $('#' + hash);
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, 1000);
}
I got a working code.
here, I am triming slash from window.location.hash and that solves my problem.Please vote if helps you.
(function($){
var jump=function(e)
{
if (e){
e.preventDefault();
var target = $(this).attr("href");
}else{
var target = location.hash.replace('/','');
}
$('html,body').animate(
{
scrollTop: $(target).offset().top
},1,function()
{
location.hash = target;
});
}
$('html, body').hide()
$(document).ready(function()
{
$('a[href^=#]').bind("click", jump);
if (location.hash){
setTimeout(function(){
$('html, body').scrollTop(0).show()
jump()
}, 0);
}else{
$('html, body').show()
}
});
})(jQuery)
I am creating a Phonegap app and I really want to avoid jqm in the project, because the css is just blowing up the whole html code. So I can change the pages with
window.location = "profiles.html";
but that's a pretty hard cut in the user experience and I really want to change this to something more smooth. Are there any possibilities to enhance the changepage process, maybe with css or something?
You can use a fade effect.
var speed = 'slow';
$('html, body').hide();
$(document).ready(function() {
$('html, body').fadeIn(speed, function() {
$('a[href], button[href]').click(function(event) {
var url = $(this).attr('href');
if (url.indexOf('#') == 0 || url.indexOf('javascript:') == 0) return;
event.preventDefault();
$('html, body').fadeOut(speed, function() {
window.location = "profiles.html";
});
});
});
});
See it in action (jsFiddle)
i'm working on a website and wondering if there is any code that I can add to a "Fixed in Window" Item that will be visible once a visitor has scrolled 200 Pixels, for example.
Couldn't find anything specifically for this in any other questions.
Cheers
My bad, I found this, perhaps this is what I'm looking for Use jQuery to show a div only when scroll position is between 2 points. Correct me if I'm wrong :-)
You can detect scrolling position with use of j Query and then if scroll is moved hide your div. Below given is example code. please review it and work on it little bit according to your requirement it is working fine with me.
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function() {
$.fn.scrollBottom = function() {
return $(document).height() - this.scrollTop() - this.height();
};
var $el = $('#sidebar>div');
var $window = $(window);
var top = $el.parent().position().top;
$window.bind("scroll resize", function() {
var gap = $window.height() - $el.height() - 10;
var visibleFoot = 172 - $window.scrollBottom();
var scrollTop = $window.scrollTop()
if (scrollTop < top + 10) {
$el.css({
top: (top - scrollTop) + "px",
bottom: "auto"
});
} else if (visibleFoot > gap) {
$el.css({
top: "auto",
bottom: visibleFoot + "px"
});
} else {
$el.css({
//use your css property here if you want to display none a div
display: none,
bottom: "auto"
});
}
}).scroll();
});
});//]]>
</script>
I am having a webpage with 3 slider and one popup box. For sliders i used jQuery.noConflict() so that is working fine. but in the case of popup box if i am using jQuery.noConflict() its not working.
If i am not using jQuery.noConflict() then my slider wont work.
popup html
<div id="popupContact">
<a id="popupContactClose"><img src="close.gif" alt="close"/></a>
<div id="contactArea"></div>
</div>
<div id="backgroundPopup"></div>
popup jquery
var popupStatus = 0;
function loadPopup(){
if(popupStatus==0){
$("#backgroundPopup").css({
"opacity": "0.2"
});
$("#backgroundPopup").fadeIn("slow");
$("#popupContact").fadeIn("slow");
popupStatus = 1;
}
}
function disablePopup(){
if(popupStatus==1){
$("#backgroundPopup").fadeOut("slow");
$("#popupContact").fadeOut("slow");
popupStatus = 0;
}
}
function centerPopup(){
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupHeight = $("#popupContact").height();
var popupWidth = $("#popupContact").width();
$("#popupContact").css({
"position": "absolute",
"top": windowHeight/2-popupHeight/2,
"left": windowWidth/2-popupWidth/2
});
$("#backgroundPopup").css({
"height": windowHeight
});
}
var $rp = jQuery.noConflict();
$rp(document).ready(function(){
$rp("#buttonpop").click(function(){
centerPopup();
loadPopup();
});
$rp("#popupContactClose").click(function(){
disablePopup();
});
$rp("#backgroundPopup").click(function(){
disablePopup();
});
$rp(document).keypress(function(e){
if(e.keyCode==27 && popupStatus==1){
disablePopup();
}
});
});
Note: This popup jquery is an external jquery and i am using <script src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" type="text/javascript"></script> also.
Thank you.
If you use jQuery.noConflict(), that prevents jQuery from aliasing jQuery as $. However your popup code is still trying to use $.
Either reference jQuery, not $, or wrap your code in a closure and reference $ as a local variable within the closure, i.e.
(function($) {
//popup code here
})(jQuery);