Enhancing your images with a light box effect

Struggling with CSS and JQuery, I'm attempting to integrate Magnific Popup into my app. Utilizing the https://github.com/joshuajansen/magnific-popup-rails, I find that the code I insert into the view is displaying on the page directly. After following the instructions on Github, I included the necessary code in the view.

<h1><%= @user.username %></h1>

<div class="parent-container">

$(document).ready(function() {
  $('.parent-container').magnificPopup({
    delegate: 'a',
    type: 'image'
  });   
});

<% @user.photos.each do |photo| %>
    <%= link_to image_tag(photo.image_url(:thumb)), photo.image_url%>

</div>
<% end %>

Answer №1

My expertise with ruby on rails is somewhat limited, but I believe it's essential to enclose your javascript within a script tag as shown below:

<script type="text/javascript">
    $(document).ready(function() {
        $('.parent-container').magnificPopup({
            delegate: 'a',
            type: 'image'
        });   
    });
</script>

<div class="parent-container">
    <% @user.photos.each do |photo| %>
        <%= link_to image_tag(photo.image_url(:thumb)), photo.image_url%>
</div>

Answer №2

Josh Mein's response is accurate, however, for adherence to w3c guidelines and positioning the javascript in the head of the document, you may also take into account:

<% content_for :head do %>
  <script type='text/javsacript'>
    $(document).ready(function() {
      $('.parent-container').magnificPopup({
        delegate: 'a',
        type: 'image'
      });   
    });
  </script>
<% end %>

... and ensure that in your layout, you include <%= yield :head %> in the head section

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Header alignment issue in JQGrid

Utilizing 4.4.4 - jQuery Grid and <= jquery 1.8.2, this is how I set up my jqgrid: function FAGrid() { var url1 = 'URL'; $("#FAList").jqGrid({ url: url1, datatype: 'json', mtype: 'POST', ...

Guide on extracting an Array from JSON in a $.ajax response

I received a JSON value that was converted from an array in the AJAX response. {"Text":"Please provide a value","Email":"Please provide a value"} My goal is to extract the response JSON and display it within a div using $(div).html(): Text-Please provid ...

Troubleshooting problem with static divs not scrolling properly within an iframe on iOS devices

I'm encountering an issue with iOS scrolling on my iPad. All other platforms I've tested work perfectly fine except for this one. While I have a love-hate relationship with iOS, I managed to make the entire iframe content scroll within the parent ...

The dropdown menu in Bootstrap 4 has non-functional links

Attempting to create a mega menu using Bootstrap. Started with dropdown menu code and made some modifications, but encountering issues where the links in the dropdown don't work properly unless opened in a new tab. When clicking on a link, it closes t ...

Can you explain the meaning of -ms-value?

I recently came across a solution for an issue specific to IE, and it worked perfectly for me. You can find the solution here: Change IE background color on unopened, focused select box However, I'm still puzzled about the meaning of -ms-value. Any i ...

Issue with integrating CrunchBase API and making AJAX requests using jQuery's $.getJSON method

Can someone help me figure out why my attempt to display an alert with the "name" is not working correctly? $(document).ready(function() { $.getJSON("http://api.crunchbase.com/v/1/companies/permalink?name=Google", function(data) { alert( ...

Increase the height of the div element by a specified number of

Is there a way to dynamically expand a div's height using CSS without knowing its initial height? I want to increase the height by a specific amount, such as "x px taller" regardless of its starting size. For example, if the div starts at 100px tall, ...

Design a logo to serve as the main button on the navigation bar of the

I've been experimenting with adding a logo instead of the 'Home' text in my navigation bar, but I'm not sure where to implement it. Should I add it within #nav or separately? Also, I want the nav bar to stay fixed without overlapping on ...

Customize Your Wordpress Category Title with a Background Image

Within my wordpress template, there is a module known as new_boxs. The code snippet below shows that this module displays on the page six times, with two columns wide and three rows down: foreach($data_cat as $cat){ ?> <div class="span6"> ...

Resolving problems with PHP source and JSON in jQuery autocomplete feature

My goal is to implement a jquery autocomplete feature with PHP, but I am facing an issue with the data structure required for each record in the autocomplete. Currently, I have successfully achieved this without PHP using the following code: $(function() ...

Effortlessly navigate between Formik Fields with automated tabbing

I have a component that validates a 4 digit phone code. It functions well and has a good appearance. However, I am struggling with the inability to autotab between numbers. Currently, I have to manually navigate to each input field and enter the number. Is ...

Ways to conceal #div element from displaying in the href attribute within the anchor tag

My anchor tag has an href attribute that looks like this: <a onclick='loadReview(\"" + strexternalURL + "\");' href='#productName1'. When clicking on it, the URL appears as http://localhost:54986/Dealerlist.aspx#productName ...

The toggler in Bootstrap 5's Navbar menu is experiencing difficulties opening on mobile browsers

Just arrived here for the first time. Encountering an issue with my Bootstrap 5.1 Navbar menu. Background info: My website is hosted locally via Xampp with php pages for forms and cookies. Everything functions perfectly on desktop. Checked responsiveness o ...

Ways to customize the default home icon

My current theme is Redmond, but I would like to change the color of the home icon in the breadcrumb. To achieve this, I have included the hot-sneaks theme in the pom.xml file: <dependency> <groupId>org.primefaces.themes</groupId&g ...

Exploring PHP Queries with Div Classes

I'm in desperate need of assistance. I'm currently working on putting together a website and pulling data from a mysql database. The specific div that I'm trying to populate is provided below. The issue I'm facing is how do I integrate ...

The text becomes jumbled and messy when the browser is resized

I came across this fantastic header design > https://codepen.io/rm/pen/ldhon <. However, I noticed that when the window is resized, the text ends up overlapping. In the demo provided, resizing the window too much causes the issue to become apparent, ...

Tips on integrating Bootstrap and jQuery into an Angular 2 project

What is the recommended method for utilizing a bootstrap plugin like Eonasdan/bootstrap-datetimepicker in Angular2? I am using jspm in my configuration and have successfully installed Eonasdan/bootstrap-datetimepicker. Next, I include the JavaScript in my ...

Unable to remove the selected option value using Jquery

I have encountered an issue with clearing the select option value dynamically using Jquery in my code. Below is a brief explanation of the setup: report.php: <div class="col-sm-4"> <select class="form-control selectized" data-live-search="true" ...

The overflowing pop-up container

When I trigger a popup that dynamically generates a fixed background with a centered dialog, it works well. However, on smaller monitors like laptops, tablets, and phones, the content can overflow beyond the visible area due to its fixed container, making ...

Center the "td" elements within a table

I am struggling with a design that involves two tables placed side by side within a main table. <table> <tr> <td style="width: 50%"> <table id="table1"> <tr><td></td></tr> <tr><td></t ...