I am making a one page website and would like to show or hide a button dependent on the section id or element that is visible in the view port.
I have a home button that i need to show when the user scrolls down past what is technically the homepage. I am using scrollify to snap scrolls to section with a height value of 100vh.
I have a function to detect if the element in question is in view but the button displays or hides relevant to the page that is loaded not by seaching for the element that would tell the button to show or hide.
I am using the div class text to determine whether to show or hide the home button is there a way to make jquery update and re engage the function when the user has scrolled or moved the page? the pages in question are live on www.testsiteclash.co.uk
Thanks
jquery
$.fn.inView = function(inViewType){
var viewport = {};
viewport.top = $(window).scrollTop();
viewport.bottom = viewport.top + $(window).height();
var bounds = {};
bounds.top = this.offset().top;
bounds.bottom = bounds.top + this.outerHeight();
switch(inViewType){
case 'bottomOnly':
return ((bounds.bottom <= viewport.bottom) && (bounds.bottom >= viewport.top));
case 'topOnly':
return ((bounds.top <= viewport.bottom) && (bounds.top >= viewport.top));
case 'both':
return ((bounds.top >= viewport.top) && (bounds.bottom <= viewport.bottom));
default:
return ((bounds.top >= viewport.top) && (bounds.bottom <= viewport.bottom));
}
};
$(document).ready(function(){
if($('.text').inView( 'both' ) == true ){
$('.home-btn').css('display','none');
}else if($('#section_1').inView( 'both' ) == false ) {
$('.home-btn').css('display','block');
}
});
Html/php
<?php get_header(); ?>
<article id="section_1">
<section class='section' data-section-name="Devon Food Movement">
<div class="container">
<div class="logo">
<div class="logo-image">
</div>
</div>
<div class="text">
<h1>Devon Food Movement</h1>
<h2>Website under construction <br class="textbreak">follow us below on ...</h2>
</div>
<div class="icons">
<div class="icon1"></div>
<div class="icon2"></div>
<div class="icon3"></div>
<div class="icon3m"></div>
</div>
</div>
</section>
</article>
<article id="section_2">
<section class='section' data-section-name="Contact">
<?php
if (have_posts()) :
while (have_posts()) : the_post();
get_template_part('form');
endwhile;
else:
echo '<p>No Content found</p>';
endif;
?>
</section>
</article>
<div class="home-btn"><i class="fas fa-home"></i></div>
</body>
</html>
Added scroll to the function all works now, noob alert haha!
$(document).scroll(function(){
if($('.text').inView( 'both' ) == true ){
$('.home-btn').css('display','none');
}else if($('#section_1').inView( 'both' ) == false ) {
$('.home-btn').css('display','block');
}
});
Here is a simplistic example that shows how to use the window.scroll event to detect whether to show/hide a button.
var currPos=1, lastPos=0, scrollDir='dn';
var triggerPos = $('#myTrigger').offset().top;
$(window).scroll(function(){
currPos = $(window).scrollTop();
$('#d2').html(currPos); //unnecc - displays current scroll position for you
scrollDir = (currPos > lastPos) ? 'dn' : 'up';
lastPos = currPos;
if (scrollDir=='dn'){
if (currPos > triggerPos) $('#btnSpecial').fadeIn(100);
}else{
if (currPos < triggerPos) $('#btnSpecial').fadeOut(100);
}
});
.dPage{width:100vw;height:100vh;}
#d1{background:palegreen;}
#d2{background:bisque;}
#d3{background:lightpink;}
#d4{background:purple;}
#d5{background:lightblue;}
#btnSpecial{display:none;position:fixed;top:30px;right:100px;padding:10px;background:dodgerblue;color:white;font-size:2.3rem;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dPage" id="d1"></div>
<div class="dPage" id="d2"><div id="myTrigger"></div></div>
<div class="dPage" id="d3"></div>
<div class="dPage" id="d4"></div>
<div class="dPage" id="d5"></div>
<button id="btnSpecial">La Button</button>
Related
i have this problem, and need your help to resolve it. I use MultiStep Checkout form, and all seems fine, except fields validation. I have 3 steps
Billing > Payment > Order Confirmation
Issue is that when left empty required field into "Billing" step, Woocommerce allow me to go to "Payment" step, and there show me error that required field is not filled. I want that check to be executed in "Billing" step. I checked into form-checkout.php, and found that navigation is made by this JS:
<?php if ($thegem_checkout_type == 'multi-step'): ?>
<script>
(function($) {
function active_checkout_tab($tab, isinit) {
if ($tab.length == 0 || ($tab.hasClass('active') && !isinit)) {
return false;
}
$tab.parent().find('.checkout-step').removeClass('active before-active');
$tab.addClass('active');
$tab.prev('.checkout-step').addClass('before-active');
var tab_id = $tab.data('tab-id');
$('.checkout-contents').removeClass('active');
$('.checkout-contents[data-tab-content-id="' + tab_id + '"]').addClass('active');
window.location.hash = '#' + tab_id;
}
var m = window.location.hash.match(/#checkout\-(.+)/);
if (m && $('.checkout-steps .checkout-step[data-tab-id="checkout-' + m[1] + '"]').length == 1) {
active_checkout_tab($('.checkout-steps .checkout-step[data-tab-id="checkout-' + m[1] + '"]'), true);
} else {
active_checkout_tab($('.checkout-steps .checkout-step:first'), true);
}
$('.checkout-steps .checkout-step').not('.disabled').click(function() {
active_checkout_tab($(this), false);
});
})(jQuery);
</script>
and this is php code:
<?php if ($thegem_checkout_type == 'multi-step'): ?>
<div class="checkout-steps <?php if(is_user_logged_in()): ?>user-logged<?php endif; ?> clearfix">
<?php if(is_user_logged_in() || 'no' === get_option( 'woocommerce_enable_checkout_login_reminder' )): ?>
<div class="checkout-step active" data-tab-id="checkout-billing"><?php esc_html_e('1. Facturation','thegem'); ?></div>
<div class="checkout-step" data-tab-id="checkout-payment"><?php esc_html_e('2. Paiement','thegem'); ?></div>
<div class="checkout-step disabled" data-tab-id="checkout-confirmation"><?php esc_html_e('3. Confirmation','thegem'); ?></div>
<?php else: ?>
<div class="checkout-step active" data-tab-id="checkout-signin"><?php esc_html_e('1. Se connecter','thegem'); ?></div>
<div class="checkout-step" data-tab-id="checkout-billing"><?php esc_html_e('2. Facturation','thegem'); ?></div>
<div class="checkout-step" data-tab-id="checkout-payment"><?php esc_html_e('3. Paiement','thegem'); ?></div>
<div class="checkout-step disabled" data-tab-id="checkout-confirmation"><?php esc_html_e('4. Confirmation','thegem'); ?></div>
<?php endif; ?>
</div>
So my question is how to made fields verification on first step instead of second one?
I want to put chronoforms inside Virtuemart tabs. You can see the tabs in:
http://dabur.cl/productos/tipo-de-producto/cremas-para-peinar/crema-reparaci%C3%B3n-nocturna-de-miel-detail.html.
As you can see, the second tab is empty. That's where the form should go.
First tab: product description
Third tab: Disqus
The contents of the tabs are in a default.php file, but I don't know what is the right syntax to call the form.
<div id="myTabContent" class="tab-content">
<?php if (!empty($this->product->product_desc)) { ?>
<div class="tab-pane desc fade active in" id="desc" >
<div class="product-description">
<?php /** #todo Test if content plugins modify the product description */ ?>
<?php echo $this->product->product_desc; ?>
</div>
</div>
<?php } // Product Description END ?>
<div class="tab-pane review fade" id="form" >
HERE IS WHERE CHRONOFORM SHOULD BE!!!
</div>
<div id="comentarios" class="tab-pane comentarios fade">
<div id='disqus_thread'></div>
<script type='text/javascript'>
var disqus_shortname = 'dabur-cl'; // required: replace example with your forum shortname
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
</div><!-- //tab3 -->
</div>
</div> <!--/. products-desc-tab-->
Already find out. Virtuemart supports chronoform plugin, so the code is:
<div class="tab-pane review fade" id="form" >
<?php echo JHTML::_('content.prepare', '{chronoforms6}form-name{/chronoforms6}');?
</div>>
What i want to achieve
I want to be able to shift seamlessly through the different posts in the sidebar on this website, while having using a custom post template for my posts. And by seamlessly i mean that the screen doesn't go white for a second every time you navigate it.
My problem
single.php or single-{slug}.php does not work, since the URL doesn't change while navigating the sidebar
Plugins i am using
Custom Post Type UI
Ajax Content Renderer
jQuery script for my sidebar
$(document).ready(function() {
$("#sidebar li:has(a)").click(function() {
var page = $("a:first",this).attr("href");
$("#content").load(page);
return false;
});
});
HTML & PHP for sidebar and content area
<div class="row"> <!-- Main content row -->
<div class="col-md-4" id="sidebar">
<?php
if (is_page('gulvservice')){
wp_nav_menu(array('menu'=>'gulvservice' ));
} elseif (is_page('malerservice')) {
wp_nav_menu(array('menu'=>'malerservice' ));
} elseif (is_page('industrilakering')) {
wp_nav_menu(array('menu'=>'industrilakering' ));
}
?>
</div>
<div class="col-md-8" id="content">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="title"><h1>
<?php the_title(); ?>
</h1></div>
<div class="div">
<?php
if( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
</div>
<?php endwhile; endif; ?>
</div>
</div>
</div> <!-- Main Content -->
Need to prevent the event on the <a>. So it would be better to move the selector to those elements
Try:
$(document).ready(function() {
$("#sidebar li a").click(function() {
var page = $(this).attr("href");
$("#content").load(page);
return false;
});
});
I need to show the content of post in wordpress. Basically I need to show a div on click, but only the current post. I have the title and the excerpt looping on the page to show all post. I want to show the content of only the current title post clicked. Example below is what I currently have.
$(document).on('click', '.post-id', function() {
$(myClass, ".post-content").show();
});
<div class="post-full">
<h2 class="post-id<?php //echo get_the_ID(); ?>"><?php the_title(); ?><div class="close">Close</div></h2>
<div class="post-excerpt"><h3><?php the_excerpt(); ?></h3></div>
<div class="post-content"><?php the_content(); ?></div>
</div>
If i understand correctly
$(document).on('click', '.post-id', function()
{
var content = $(this).nextAll('.post-content');
if (content.is(':visible'))
{
content.hide();
}
else
{
$('.post-content:visible').hide();
content.show();
}
});
Here is the JavaScript I use to animate slider (fade effect) of the content I read from database:
<script language="javascript">
jQuery(document).ready(function ()
{
var terms = ["span_1","span_2"];
var i = 0;
function rotateTerm() {
jQuery("#text-content").fadeOut(200, function() {
jQuery(this).text(jQuery('#text-slider .'+terms[i]).html()+i).fadeIn(200);
});
jQuery("#title-content").fadeOut(200, function() {
jQuery(this).text(jQuery('#title-slider .'+terms[i]).html()+i).fadeIn(200);
i == terms.length - 1 ? i=0 : i++;
});
}
rotateTerm();
setInterval(rotateTerm, 1000);
});
</script>
And here is the PHP code I use:
<?php
if (!empty($testLst)) :
$num=1;
foreach($testLst as $key=>$item):
$item->slug = $item->id;
$item->catslug = $item->catid ;
?><div id="hidden-content" style="display:none;">
<div id="title-slider">
<span class="<?php echo 'span_'.$num; ?>">
<h4><a href="<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($item->id, $item->catid)); ?>">
<?php echo $item->title; ?></a>
</h4>
</span>
</div>
<div id="text-slider">
<span class="<?php echo 'span_'.$num; ?>">
<p>
<?php
$concat=array_slice(explode(' ',$item->introtext),0,20);
$concat=implode(' ',$concat);
echo $concat."...";
?>
</p>
</span>
</div></div>
Learn more >></p>
<?php
$num++;
endforeach;
endif;
?>
<div id="title-content">
</div>
<div id="text-content">
</div>
And here is a JSFiddle page reproducing what I would like to do.
My problem is that I am getting data that still has HTML tags, however I would like the output to have my CSS styles.
You could clone the node, and set that to be the new content of the target elements, to keep everything in jQuery objects, but personally, I'd use the .outerHTML property.
I've updated your fiddle to show you what I mean: I've changed the .text(...set content here) to .html(), because we're injecting HTML content. Then, I added [0] at the end of your selector, to return the raw element reference, which gives access to all standard JS properties and methods an element has, and just went ahead and fetched the outerHTML... easy-peasy