Why image is not display on full screen? - javascript

I am trying to display image on full screen on image click. In other words o click of image it will show in full screen
I tried like this but not working
$(".imageBlock").click(function () {
$('.imageBlock').css({'width':'100%','height':'100%','z-index':'9999'})
});
https://jsbin.com/hayalobixo/1/edit?html,css,js,output
$(function() {
var counter = 0;
$('.currentPage').text(counter+1);
$('#pre').prop('disabled', true);
$('#next').click(function() {
if (counter < 4) {
counter++;
$('.imageBlock').hide();
$('.imageBlock').eq(counter).show();
}
$('.currentPage').text(counter+1);
})
$('#pre').click(function() {
if (counter > 0) {
counter--;
$('.imageBlock').hide();
$('.imageBlock').eq(counter).show();
}
$('.currentPage').text(counter+1);
})
$('#next, #pre').click(function() {
if (counter == 0) {
$('#pre').prop('disabled', true);
} else if (counter == 4) {
$('#next').prop('disabled', true);
} else {
$('#pre').prop('disabled', false);
$('#next').prop('disabled', false);
}
})
$(".imageBlock").click(function () {
$('.imageBlock').css({'width':'100%','height':'100%','z-index':'9999'})
});
})

CSS:
div.imageBlock.full{
position: fixed;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
JS:
$(".imageBlock").click(function () {
$('.imageBlock').addClass("full");
});

You can use - div as absolute positioned
$(function() {
$(".imageBlock").click(function () {
$(this).css({'width':'100%','height':'100%','z-index':'9999','position':'absolute'})
});
https://jsbin.com/kowogukave/edit?html,css,js,console,output

Related

Conditional addEventListener when div ID is at top of window

I am trying to use addEventListener when the user scroll into view <div id="container">. I have done so by scroll height but my attempts to addEventListener when <div> is on the top of the window.
handleHeaderStuck = () => {
if (this.innerContainer.scrollTop <= 500 && this.state.isStuck === true) {
this.setState({isStuck: false});
}
else if (this.innerContainer.scrollTop >= 500 && this.state.isStuck !== true) {
this.setState({isStuck: true});
}
}
This will setState when scrolled 500px.
How can I replace the condition of 500px to be set when the user is with id="container" as the top of the window? Also replace isStuck state to isStuckBottom when the user reaches the bottom of the div.
The full code is
constructor(props) {
super(props)
this.state = {
isStuck: false,
}
this.handleHeaderStuck = this.handleHeaderStuck.bind(this)
}
innerContainer = null
componentDidMount () {
this.innerContainer.addEventListener("scroll", this.handleHeaderStuck);
}
componentWillUnmount () {
this.innerContainer.removeEventListener("scroll", this.handleHeaderStuck);
}
handleHeaderStuck = () => {
if (this.innerContainer.scrollTop <= 500 && this.state.isStuck === true) {
this.setState({isStuck: false});
}
else if (this.innerContainer.scrollTop >= 500 && this.state.isStuck !== true) {
this.setState({isStuck: true});
}
}
var atTop = false;
var atBotton = false;
document.addEventListener("scroll", e => {
var elemInfo = document.getElementById("container").getClientRects()[0];
if (parseInt(elemInfo.bottom) >= window.innerHeight)
{
if (!atBottom){
console.log("I touche the bottom")
document.getElementById("information").innerHTML = "mouse reached the bottom";
atBottom = true;
}
atTop = false;
} else if (parseInt(elemInfo.top) <= 0) {
if (!atTop) {
console.log("Mouse at top")
document.getElementById("information").innerHTML = "I touched the top";
atTop = true;
}
atBottom = false;
} else {
console.log("I touched the nothing")
}
});
body {
margin: 0px;
padding: 0px;
border: 0px;
}
.first, .bottom {
width: 100%;
height: 200px;
background-color: Green;
}
#container {
width: 100%;
background-color: Yellow;
margin: 0px;
padding: 0px;
border: 1 px solid black;
height: 200px;
}
#infomation {
font-size: 50px;
}
<div class="example">
</div>
<div id=container>
<div id=information></div></div>
<div class="bottom">
</div>
You add an "scroll" onto document and check if the div is currently at 0 relative to the current viewport. To get the position relative to the current view of the user you can use getBoundingClientRect(). You can implement it in this way:
document.addEventListener("scroll", () => {
div = document.querySelector("#container")
if(div.getBoundingClientRect().top == 0) {
//Do what you need to do here, this executes whenever the
//top of the container is at the top of the screen.
}
})

Set interval to make auto-slide functionality(Magento Site)

the below-following code is written by freelancer programmer for the silde banner for our Magento website. This is only for slide banner when the customer clicks the pager navigation menu; it slides to next banner. I want to set Interval for this so that It can automatically slide with clicking pager button. Thank you!!!
function initialize_banner_slider(banner_id) {
if ($(banner_id).size() <= 0) return;
var make_center = function(center) {
center.removeClass("on_right").removeClass("on_left").addClass("on_center");
$("body").removeClass("theme-light").removeClass("theme-dark").addClass("theme-"+center.data("theme"));
center.find(".fadeup").each(function() {
$(this).hide().css("top", (parseInt($(this).data("pos-y"))/750*100+100) + "%");
});
$(banner_id + " ul.banner_pager li").removeClass("active");
$($(banner_id + " ul.banner_pager li")[center.index()]).addClass("active");
setTimeout(function() {
center.find(".fadeup").each(function() {
$(this).show().animate({"top": "-=100%"});
/* $(this).css("top", parseInt($(this).data("pos-y"))); */
});
}, 600);
}
var move_full_card_left = function(banner_id) {
if ($(banner_id).find(".on_right").size() > 0) {
$(banner_id).find(".on_center").removeClass("on_center").addClass("on_left");
make_center( $(banner_id).find(".on_right").first() );
if ($(banner_id).find(".on_right").size() == 0) {
// hide arrow
$(banner_id).find(".move_right").hide();
} else {
// show arrow
$(banner_id).find(".move_right").show();
}
$(banner_id).find(".move_left").show();
}
return false;
}
var move_full_card_right = function(banner_id) {
if ($(banner_id).find(".on_left").size() > 0) {
$(banner_id).find(".on_center").removeClass("on_center").addClass("on_right");
make_center( $(banner_id).find(".on_left").last() );
if ($(banner_id).find(".on_left").size() == 0) {
// hide arrow
$(banner_id).find(".move_left").hide();
} else {
// show arrow
$(banner_id).find(".move_left").show();
}
$(banner_id).find(".move_right").show();
}
return false;
}
$(banner_id).find(".move_left").click(function() {
return move_full_card_right(banner_id);
});
$(banner_id).find(".move_right").click(function() {
return move_full_card_left(banner_id);
});
for (var i=0, l=$(banner_id+" > ul.slider > li").size(); i<l; i++) {
var pager = $("<li></li>");
pager.on("click", function() {
var index = $(this).index();
$(banner_id+" > ul.slider > li").each(function(ndx, val) {
if (ndx < index) {
$(this).removeClass("on_center").removeClass("on_right").addClass("on_left");
} else if (ndx > index) {
$(this).removeClass("on_center").removeClass("on_left").addClass("on_right");
} else if (ndx == index) {
make_center($(this));
}
});
});
pager.appendTo($(banner_id + " ul.banner_pager"));
}
var first = true;
$(banner_id+" > ul.slider > li").each(function(elem) {
if (first) {
make_center( $(this) );
first = false;
} else {
$(this).addClass("on_right");
}
$(this).on("swipeleft", function() {
return move_full_card_left(banner_id);
}).on("swiperight", function() {
return move_full_card_right(banner_id);
});
$(this).css("background-image", "url("+$(this).data("background-image")+")");
});
if ($(banner_id+" > ul.slider > li").size() < 2) {
$(banner_id).find(".move_right").hide();
}
$(banner_id).find(".move_left").hide();
}
function initialize_parallax() {
$(".responsive_wrapper").each(function() {
var base_width = $(this).data("width");
var base_height = $(this).data("height");
$(this).css({
"max-width": base_width+"px",
"max-height": base_height+"px"
});
$(this).find(".responsive").each(function() {
$(this).css({
"width": $(this).data("width")/base_width*100 + "%",
"height": $(this).data("height")/base_height*100 + "%",
"left": $(this).data("pos-x")/base_width*100 + "%",
"top": (parseInt($(this).data("pos-y"))/base_height*100) + "%",
});
});
});
}
$(document).ready(function() {
/* parallax positioning */
// $(".verus-cms .parallax").insertAfter( $(".page-header") );
$("#product-list-toolbar2").prependTo(".col-main");
initialize_parallax();
initialize_banner_slider("#top_banner");
initialize_banner_slider("#lific_banner");
You would add something like this:
setInterval(function(){move_full_card_right(banner_id);},5000);
You should be able to throw that in you document ready as long as you can get the banner_id. I don't know how you are setting the banner id, so I can't help you with that.

Nav won't change size upon scrolling

So I found this jquery code to make my nav change its size when a user begins to scroll. It doesn't appear to be working, though...
$(function(){
$('.nav').data('size','big');
});
$(window).scroll(function(){
var $nav = $('.nav');
if ($('body').scrollTop() > 0) {
if ($nav.data('size') == 'big') {
$nav.data('size','small').stop().animate({
height:'60px'
}, 600);
}
} else {
if ($nav.data('size') == 'small') {
$nav.data('size','big').stop().animate({
height:'40px'
}, 600);
}
}
Here is all the code:
http://codepen.io/DerekDev/pen/Bydgpz
So if any of you have a solution, it would be greatly appreciated. Thanks :)
try this:
$(function() {
$(window).scroll(function() {
var nav = $('.nav');
if ($(window).scrollTop() >= 0) {
if(!nav.hasClass("smallNav")) {
nav.hide();
nav.removeClass('bigNav').addClass("smallNav").fadeIn( "slow");
}
} else {
if(!nav.hasClass("bigNav")) {
nav.hide();
nav.removeClass("sml-logo").addClass('bigNav').fadeIn( "slow");
}
}
});
});
and CSS rules:
.bigNav{
height:60px;
}
.smallNav{
height:40px;
}
Firstly, you want to check the window scrollTop, not the body
Secondly, you want to change the height of the UL, not the .nav element, and you seem to have the order mixed up, you probably want 40px when it's small, and 60px when it's big !
$(window).scroll(function(){
var $nav = $('.nav');
if ($(window).scrollTop() > 0) {
console.log($nav.data('size'))
if ($nav.data('size') == 'big') {
$nav.data('size','small').find('ul').stop().animate({
height:'40px'
}, 600);
}
} else {
if ($nav.data('size') == 'small') {
$nav.data('size','big').find('ul').stop().animate({
height:'60px'
}, 600);
}
}
});
PEN

My slider doesn't work as it should after viewing 3 "screens"

I'm trying to build slider and in the first 3 "screens" it work and in the last one is doesn't. Also, is there a way to make the slider to slide and not just to show up?
the js code:
var oldnum = 0
var screen = 1;
$("#right_arrow").click(function () {
screen++;
if (screen >= 4) {
$("#right_arrow").hide();
screen = 4;
} else {
gotoright(screen);
}
});
$("#left_arrow").click(function () {
screen--;
if (screen <= 1) {
$("#left_arrow").hide();
screen = 1;
} else {
gotoleft(screen);
}
});
jwerty.key('arrow-right', function () {
screen++;
if (screen >= 4) {
$("#right_arrow").hide();
screen = 4;
} else {
gotoright(screen);
}
});
jwerty.key('arrow-left', function () {
screen--;
if (screen <= 1) {
$("#left_arrow").hide();
screen = 1;
} else {
gotoleft(screen);
}
});
function gotoright(num) {
if (num <= 0 && num >= 4) {
$("#b_" + num).show().animate({
"opacity": 1
}, 400, function () {});
} else {
oldnum = num - 1;
$("#b_" + num).show().animate({
"opacity": 1
}, 400, function () {
$("#b_" + oldnum).hide().css({
"opacity": 0
});
});
}
}
function gotoleft(num) {
if (num <= 0 && num >= 4) {
$("#b_" + num).show().animate({
"opacity": 1
}, 400, function () {});
} else {
oldnum = num + 1;
$("#b_" + num).show().animate({
"opacity": 1
}, 400, function () {
$("#b_" + oldnum).hide().css({
"opacity": 0
});
});
}
}
here is the full code with the html and css:
http://jsfiddle.net/k6xdq/1/
I want that it will work like http://tobiasahlin.com/spinkit/
I think you should check if screen is == 4 before you ++ it.
Right now you go from 3 to 4 then go to an if statement that only hides the arrow.
$("#right_arrow").click(function () {
screen++;
if (screen >= 4) {
$("#right_arrow").hide();
screen = 4;
} else {
gotoright(screen);
}
});
What you want is to gotoright() and if screen is >= 4, hide() it.
$("#right_arrow").click(function () {
screen++;
if (screen >= 4) {
$("#right_arrow").hide();
screen = 4;
gotoright(screen);
} else {
gotoright(screen);
}
});

Navigating long un-ordered list

I have this long list with overflow: auto to scroll through it, i set it up for keyboard navigation, the problem is that when using the keyboard it doesn't scroll correctly!
check this jsFiddle
$('ul').keydown(function (e) {
if (e.keyCode == 38) { // up
var selected = $(".selected");
$listItems.removeClass("selected");
if (selected.prev().length == 0) {
selected.siblings().last().addClass("selected");
} else {
selected.prev().addClass("selected");
}
}
if (e.keyCode == 40) { // down
var selected = $(".selected");
$listItems.removeClass("selected");
if (selected.next().length == 0) {
selected.siblings().first().addClass("selected");
} else {
selected.next().addClass("selected");
}
}
})
});
$listItems.click(function () {
if ($(this).is('.selected')) {
return true;
} else {
$('li').removeClass('selected');
$(this).addClass('selected');
}
the behavior i'm looking for is the same behavior of the element when scrolling through a long list of elements using the keyboard this plugin SelectBoxIt show's what i'm looking for.
you can use this code instead, i used animate function to navigate inside the div if the list exceed the width of the ul tag :
http://jsfiddle.net/goldendousa/p6243/13/
$('ul').focus(function() {
if ($('ul li').is('.selected')) {
$('ul li').first().removeClass('selected');
} else {
$('ul li').first().addClass('selected');
}
});
$('ul').keydown(function(e) {
if (e.keyCode == 38) { // up
e.preventDefault();
var selected = $(".selected");
$("ul li").removeClass("selected");
if (selected.prev().length == 0) {
selected.siblings().last().addClass("selected");
var selectedTopOffset = selected.siblings().last().offset().top;
$('div').animate({
scrollTop: selectedTopOffset
}, 200);
} else {
selected.prev().addClass("selected");
var selectedTopOffset = $("div").scrollTop() + selected.position().top - $("div").height()/2 + selected.height()/2;
$('div').animate({
scrollTop: selectedTopOffset
}, 200);
}
}
if (e.keyCode == 40) { // down
e.preventDefault();
var selected = $(".selected");
$("ul li").removeClass("selected");
if (selected.next().length == 0) {
selected.siblings().first().addClass("selected");
if (selected.siblings().first().offset().top < 0) {
$('div').animate({
scrollTop: selected.siblings().first().offset().top
}, 200);
}
} else {
selected.next().addClass("selected");
var selectedTopOffset = $("div").scrollTop() + selected.position().top - $("div").height()/2 + selected.height()/2;
$('div').animate({
scrollTop: selectedTopOffset
}, 200);
}
}
});
$('li').click(function() {
if ($(this).is('.selected')) {
return true;
} else {
$('li').removeClass('selected');
$(this).addClass('selected');
}
});

Categories