HTML Div Interaction
December 13, 2024 · AI Conversations
1. **Add content**: Populate the “ with text, HTML, or dynamically generated content.
2. **Style it**: Apply CSS rules to the `items` class for design.
3. **Script interaction**: Use JavaScript to manipulate
### Step 1: Set Up Your Plugin
1. **Create a New Plugin Folder**: In your WordPress installation, navigate to `wp-content/plugins/` and create a new folder, e.g., `overlay-text-slider`.
2. **Create the Main Plugin File**: Inside your new folder, create a PHP file, e.g., `overlay-text-slider.php`, and add the following code to set up the plugin:
<?php
/*
Plugin Name: Overlay Text Slider
Description: A WPBakery widget for overlay text slider.
Version: 1.0
Author: Your Name
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
// Include necessary files
add_action('vc_before_init', 'overlay_text_slider_vc');
function overlay_text_slider_vc() {
vc_map(array(
'name' => __('Overlay Text Slider', 'text-domain'),
'base' => 'overlay_text_slider',
'category' => __('Content', 'text-domain'),
'params' => array(
array(
'type' => 'attach_images',
'heading' => __('Images', 'text-domain'),
'param_name' => 'images',
'description' => __('Select images for the slider.', 'text-domain'),
'admin_label' => true,
),
array(
'type' => 'textarea_html',
'heading' => __('Overlay Text', 'text-domain'),
'param_name' => 'content',
'description' => __('Enter overlay text for the slider.', 'text-domain'),
),
array(
'type' => 'textfield',
'heading' => __('Slider Height', 'text-domain'),
'param_name' => 'slider_height',
'description' => __('Set the height of the slider (e.g., 400px).', 'text-domain'),
),
),
));
}
add_shortcode('overlay_text_slider', 'overlay_text_slider_shortcode');
function overlay_text_slider_shortcode($atts, $content = null) {
$atts = shortcode_atts(array(
'images' => '',
'slider_height' => '400px',
), $atts);
$images = explode(',', $atts['images']);
ob_start();
?>
<div class="overlay-text-slider" style="height: <?php echo esc_attr($atts['slider_height']); ?>;">
<?php foreach ($images as $image_id) : ?>
<?php $image_url = wp_get_attachment_image_url($image_id, 'full'); ?>
<div class="slide" style="background-image: url('<?php echo esc_url($image_url); ?>');">
<div class="overlay-text">
<?php echo do_shortcode($content); ?>
</div>
</div>
<?php endforeach; ?>
</div>
<style>
.overlay-text-slider {
position: relative;
overflow: hidden;
}
.slide {
width: 100%;
height: 100%;
background-size: cover;
background-position: center;
position: relative;
display: none; /* Use JavaScript to display slides */
}
.slide.active {
display: block;
}
.overlay-text {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #fff;
text-align: center;
background-color: rgba(0, 0, 0, 0.5); /* Semi-transparent background */
padding: 20px;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
const slides = document.querySelectorAll('.overlay-text-slider .slide');
let currentIndex = 0;
function showSlide(index) {
slides.forEach((slide, i) => {
slide.classList.toggle('active', i === index);
});
}
function nextSlide() {
currentIndex = (currentIndex + 1) % slides.length;
showSlide(currentIndex);
}
showSlide(currentIndex);
setInterval(nextSlide, 5000); // Change slide every 5 seconds
});
</script>
<?php
return ob_get_clean();
}
### Step 2: Activate the Plugin
1. Go to the **WordPress Admin Dashboard**.
2. Navigate to **Plugins > Installed Plugins**.
3. Find **Overlay Text Slider** and click **Activate**.
### Step 3: Use the Widget in WPBakery
1. Open a page or post where you want to add the slider.
2. Click on the **+** button to add a new element in WPBakery.
3. Search for **Overlay Text Slider** in the elements list.
4. Add images and enter overlay text in the provided fields.
5. Save and publish your page.
### Step 4: Style the Slider
You may want to further customize the styles or animations. The CSS and JavaScript included in the shortcode can be modified to fit your design requirements.
### Final Thoughts
This basic example provides a starting point for creating an overlay text slider widget for WPBakery. You can expand its functionality, add options for customization, or enhance the styles as needed. If you encounter any issues or need specific features, feel free to ask!
### Step 1: Update the Plugin Code
Modify the `overlay-text-slider.php` file to include a larger and smaller image for each slide. Here’s an updated version of the plugin code:
<?php
/*
Plugin Name: Overlay Text Slider
Description: A WPBakery widget for overlay text slider with two images per slide.
Version: 1.1
Author: Your Name
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
// Include necessary files
add_action('vc_before_init', 'overlay_text_slider_vc');
function overlay_text_slider_vc() {
vc_map(array(
'name' => __('Overlay Text Slider', 'text-domain'),
'base' => 'overlay_text_slider',
'category' => __('Content', 'text-domain'),
'params' => array(
array(
'type' => 'attach_images',
'heading' => __('Larger Image', 'text-domain'),
'param_name' => 'large_image',
'description' => __('Select a larger image for the slide.', 'text-domain'),
'admin_label' => true,
),
array(
'type' => 'attach_images',
'heading' => __('Smaller Image', 'text-domain'),
'param_name' => 'small_image',
'description' => __('Select a smaller image for the slide.', 'text-domain'),
'admin_label' => true,
),
array(
'type' => 'textarea_html',
'heading' => __('Overlay Text', 'text-domain'),
'param_name' => 'content',
'description' => __('Enter overlay text for the slider.', 'text-domain'),
),
array(
'type' => 'textfield',
'heading' => __('Slider Height', 'text-domain'),
'param_name' => 'slider_height',
'description' => __('Set the height of the slider (e.g., 400px).', 'text-domain'),
),
),
));
}
add_shortcode('overlay_text_slider', 'overlay_text_slider_shortcode');
function overlay_text_slider_shortcode($atts, $content = null) {
$atts = shortcode_atts(array(
'large_image' => '',
'small_image' => '',
'slider_height' => '400px',
), $atts);
$large_image_url = wp_get_attachment_image_url($atts['large_image'], 'full');
$small_image_url = wp_get_attachment_image_url($atts['small_image'], 'full');
ob_start();
?>
<div class="overlay-text-slider" style="height: <?php echo esc_attr($atts['slider_height']); ?>;">
<div class="slide" style="position: relative;">
<div class="large-image" style="background-image: url('<?php echo esc_url($large_image_url); ?>');"></div>
<div class="small-image" style="background-image: url('<?php echo esc_url($small_image_url); ?>');"></div>
<div class="overlay-text">
<?php echo do_shortcode($content); ?>
</div>
</div>
</div>
<style>
.overlay-text-slider {
position: relative;
overflow: hidden;
}
.slide {
width: 100%;
height: 100%;
position: relative;
}
.large-image {
width: 100%;
height: 100%;
background-size: cover;
background-position: center;
position: absolute;
top: 0;
left: 0;
}
.small-image {
width: 40%; /* Adjust as needed */
height: 40%; /* Adjust as needed */
background-size: cover;
background-position: center;
position: absolute;
bottom: 10px; /* Position from the bottom */
right: 10px; /* Position from the right */
z-index: 1; /* Bring small image in front */
}
.overlay-text {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #fff;
text-align: center;
background-color: rgba(0, 0, 0, 0.5); /* Semi-transparent background */
padding: 20px;
z-index: 2; /* Overlay text above images */
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
// Add slider functionality here if you want multiple slides
});
</script>
<?php
return ob_get_clean();
}
### Step 2: Activate the Updated Plugin
1. **Go to your WordPress Admin Dashboard**.
2. Navigate to **Plugins > Installed Plugins**.
3. If the plugin is already activated, you can refresh it by deactivating and reactivating it.
### Step 3: Use the Widget in WPBakery
1. **Open a page or post** where you want to add the slider.
2. Click on the **+** button to add a new element in WPBakery.
3. Search for **Overlay Text Slider** in the elements list.
4. **Upload and select images** for both the larger and smaller image fields, and enter overlay text in the provided field.
5. Save and publish your page.
### Step 4: Adjust Styles and Functionality
You may need to adjust the CSS properties for the `.small-image` and `.large-image` classes to fit your design preferences. You can also expand the JavaScript functionality if you wish to create multiple slides, add navigation controls, or implement autoplay features.
### Final Thoughts
This setup will allow you to create a slider where each slide has one large image and one smaller image, both with overlay text. If you have further customizations or additional features in mind, feel free to ask!
### Step 1: Update the Plugin Code
Modify your `overlay-text-slider.php` file to include separate overlay text fields for the larger and smaller images. Here’s how the updated code looks:
<?php
/*
Plugin Name: Overlay Text Slider
Description: A WPBakery widget for overlay text slider with two images and separate overlay texts.
Version: 1.2
Author: Your Name
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
// Include necessary files
add_action('vc_before_init', 'overlay_text_slider_vc');
function overlay_text_slider_vc() {
vc_map(array(
'name' => __('Overlay Text Slider', 'text-domain'),
'base' => 'overlay_text_slider',
'category' => __('Content', 'text-domain'),
'params' => array(
array(
'type' => 'attach_images',
'heading' => __('Larger Image', 'text-domain'),
'param_name' => 'large_image',
'description' => __('Select a larger image for the slide.', 'text-domain'),
'admin_label' => true,
),
array(
'type' => 'attach_images',
'heading' => __('Smaller Image', 'text-domain'),
'param_name' => 'small_image',
'description' => __('Select a smaller image for the slide.', 'text-domain'),
'admin_label' => true,
),
array(
'type' => 'textarea_html',
'heading' => __('Overlay Text for Larger Image', 'text-domain'),
'param_name' => 'large_text',
'description' => __('Enter overlay text for the larger image.', 'text-domain'),
),
array(
'type' => 'textarea_html',
'heading' => __('Overlay Text for Smaller Image', 'text-domain'),
'param_name' => 'small_text',
'description' => __('Enter overlay text for the smaller image.', 'text-domain'),
),
array(
'type' => 'textfield',
'heading' => __('Slider Height', 'text-domain'),
'param_name' => 'slider_height',
'description' => __('Set the height of the slider (e.g., 400px).', 'text-domain'),
),
),
));
}
add_shortcode('overlay_text_slider', 'overlay_text_slider_shortcode');
function overlay_text_slider_shortcode($atts, $content = null) {
$atts = shortcode_atts(array(
'large_image' => '',
'small_image' => '',
'large_text' => '',
'small_text' => '',
'slider_height' => '400px',
), $atts);
$large_image_url = wp_get_attachment_image_url($atts['large_image'], 'full');
$small_image_url = wp_get_attachment_image_url($atts['small_image'], 'full');
ob_start();
?>
<div class="overlay-text-slider" style="height: <?php echo esc_attr($atts['slider_height']); ?>;">
<div class="slide" style="position: relative;">
<div class="large-image" style="background-image: url('<?php echo esc_url($large_image_url); ?>');"></div>
<div class="small-image" style="background-image: url('<?php echo esc_url($small_image_url); ?>');"></div>
<div class="overlay-text large-overlay">
<?php echo do_shortcode($atts['large_text']); ?>
</div>
<div class="overlay-text small-overlay">
<?php echo do_shortcode($atts['small_text']); ?>
</div>
</div>
</div>
<style>
.overlay-text-slider {
position: relative;
overflow: hidden;
}
.slide {
width: 100%;
height: 100%;
position: relative;
}
.large-image {
width: 100%;
height: 100%;
background-size: cover;
background-position: center;
position: absolute;
top: 0;
left: 0;
}
.small-image {
width: 40%; /* Adjust as needed */
height: 40%; /* Adjust as needed */
background-size: cover;
background-position: center;
position: absolute;
bottom: 10px; /* Position from the bottom */
right: 10px; /* Position from the right */
z-index: 1; /* Bring small image in front */
}
.overlay-text {
position: absolute;
color: #fff;
text-align: center;
background-color: rgba(0, 0, 0, 0.5); /* Semi-transparent background */
padding: 10px;
z-index: 2; /* Overlay text above images */
}
.large-overlay {
top: 20%; /* Position for the larger image overlay */
left: 10%; /* Adjust as needed */
}
.small-overlay {
bottom: 15%; /* Position for the smaller image overlay */
right: 15%; /* Adjust as needed */
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
// Add slider functionality here if you want multiple slides
});
</script>
<?php
return ob_get_clean();
}
### Step 2: Activate the Updated Plugin
1. **Go to your WordPress Admin Dashboard**.
2. Navigate to **Plugins > Installed Plugins**.
3. If the plugin is already activated, you can refresh it by deactivating and reactivating it.
### Step 3: Use the Widget in WPBakery
1. **Open a page or post** where you want to add the slider.
2. Click on the **+** button to add a new element in WPBakery.
3. Search for **Overlay Text Slider** in the elements list.
4. **Upload and select images** for both the larger and smaller image fields.
5. **Enter separate overlay text** for the larger and smaller images in the corresponding fields.
6. Save and publish your page.
### Step 4: Adjust Styles and Functionality
You can adjust the CSS properties for the overlay text positions to fit your design preferences. The positions defined in `.large-overlay` and `.small-overlay` classes can be modified to achieve the desired layout.
### Final Thoughts
This setup will create a slider where each slide has one large image and one smaller image, each with its own overlay text. If you need further adjustments or features, feel free to ask!
### Full Code for `overlay-text-slider.php`
<?php
/*
Plugin Name: Overlay Text Slider
Description: A WPBakery widget for overlay text slider with two images and separate overlay texts.
Version: 1.2
Author: Your Name
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
// Include necessary files
add_action('vc_before_init', 'overlay_text_slider_vc');
function overlay_text_slider_vc() {
vc_map(array(
'name' => __('Overlay Text Slider', 'text-domain'),
'base' => 'overlay_text_slider',
'category' => __('Content', 'text-domain'),
'params' => array(
array(
'type' => 'attach_image',
'heading' => __('Larger Image', 'text-domain'),
'param_name' => 'large_image',
'description' => __('Select a larger image for the slide.', 'text-domain'),
'admin_label' => true,
),
array(
'type' => 'attach_image',
'heading' => __('Smaller Image', 'text-domain'),
'param_name' => 'small_image',
'description' => __('Select a smaller image for the slide.', 'text-domain'),
'admin_label' => true,
),
array(
'type' => 'textarea_html',
'heading' => __('Overlay Text for Larger Image', 'text-domain'),
'param_name' => 'large_text',
'description' => __('Enter overlay text for the larger image.', 'text-domain'),
),
array(
'type' => 'textarea_html',
'heading' => __('Overlay Text for Smaller Image', 'text-domain'),
'param_name' => 'small_text',
'description' => __('Enter overlay text for the smaller image.', 'text-domain'),
),
array(
'type' => 'textfield',
'heading' => __('Slider Height', 'text-domain'),
'param_name' => 'slider_height',
'description' => __('Set the height of the slider (e.g., 400px).', 'text-domain'),
),
),
));
}
add_shortcode('overlay_text_slider', 'overlay_text_slider_shortcode');
function overlay_text_slider_shortcode($atts, $content = null) {
$atts = shortcode_atts(array(
'large_image' => '',
'small_image' => '',
'large_text' => '',
'small_text' => '',
'slider_height' => '400px',
), $atts);
$large_image_url = wp_get_attachment_image_url($atts['large_image'], 'full');
$small_image_url = wp_get_attachment_image_url($atts['small_image'], 'full');
ob_start();
?>
<div class="overlay-text-slider" style="height: <?php echo esc_attr($atts['slider_height']); ?>;">
<div class="slide" style="position: relative;">
<div class="large-image" style="background-image: url('<?php echo esc_url($large_image_url); ?>');"></div>
<div class="small-image" style="background-image: url('<?php echo esc_url($small_image_url); ?>');"></div>
<div class="overlay-text large-overlay">
<?php echo do_shortcode($atts['large_text']); ?>
</div>
<div class="overlay-text small-overlay">
<?php echo do_shortcode($atts['small_text']); ?>
</div>
</div>
</div>
<style>
.overlay-text-slider {
position: relative;
overflow: hidden;
}
.slide {
width: 100%;
height: 100%;
position: relative;
}
.large-image {
width: 100%;
height: 100%;
background-size: cover;
background-position: center;
position: absolute;
top: 0;
left: 0;
}
.small-image {
width: 40%; /* Adjust as needed */
height: 40%; /* Adjust as needed */
background-size: cover;
background-position: center;
position: absolute;
bottom: 10px; /* Position from the bottom */
right: 10px; /* Position from the right */
z-index: 1; /* Bring small image in front */
}
.overlay-text {
position: absolute;
color: #fff;
text-align: center;
background-color: rgba(0, 0, 0, 0.5); /* Semi-transparent background */
padding: 10px;
z-index: 2; /* Overlay text above images */
}
.large-overlay {
top: 20%; /* Position for the larger image overlay */
left: 10%; /* Adjust as needed */
}
.small-overlay {
bottom: 15%; /* Position for the smaller image overlay */
right: 15%; /* Adjust as needed */
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
// Add slider functionality here if you want multiple slides
});
</script>
<?php
return ob_get_clean();
}
### Step 2: Save the Plugin
1. Create a folder named `overlay-text-slider` in your WordPress `wp-content/plugins` directory.
2. Inside this folder, create a file named `overlay-text-slider.php` and paste the above code into it.
### Step 3: Activate the Plugin
1. Go to your WordPress Admin Dashboard.
2. Navigate to **Plugins > Installed Plugins**.
3. Find **Overlay Text Slider** in the list and activate it.
### Step 4: Use the Widget in WPBakery
1. Open a page or post where you want to add the slider.
2. Click on the **+** button to add a new element in WPBakery.
3. Search for **Overlay Text Slider** in the elements list.
4. Upload and select images for both the larger and smaller image fields.
5. Enter separate overlay text for the larger and smaller images in the provided fields.
6. Save and publish your page.
### Final Thoughts
This code provides a WPBakery widget for an overlay text slider featuring two images, each with separate overlay text. Adjust CSS properties as necessary to fit your design preferences. If you have further questions or need additional features, feel free to ask!